Newbie question: PyQt & Form::init()

Sven Ehret sation at web.de
Mon Sep 25 15:46:57 CEST 2006


Diez B. Roggisch wrote:

> Sven Ehret wrote:
> 
>> Hello List,
>> 
>> I am trying to learn Python and followed the tutorial at
>> http://www.cs.usfca.edu/~afedosov/qttut/. Being happy that it works, I am
>> now trying to do my own project, but I am having problems with
>> initialization of my form.
>> 
>> I want to automatically fill a couple of comboboxes upon starting the
>> program. I read on the net that I simply have to add a function
>> called »Form1.Init()« with the content I wish to have executed at start
>> time. However, it never gets executed, so it seems. When I re-write the
>> application to have the code executed at the press of a button, it works.
>> What am I doing wrong?
> 
> Show us more code, and we show you the problem. At least we can try then,
> until someone finally implements
> 
> from __future__ import mindreading
> 
> Diez

Oh, sorry. *blush*
here is my code (two files):

form1.py:
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'form1.ui'
#
# Created: Mo Sep 25 15:38:56 2006
#      by: The PyQt User Interface Compiler (pyuic) 3.14.1
#
# WARNING! All changes made in this file will be lost!


from qt import *


class Form1(QDialog):
    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        QDialog.__init__(self,parent,name,modal,fl)

        if not name:
            self.setName("Form1")



        self.pushButton1 = QPushButton(self,"pushButton1")
        self.pushButton1.setGeometry(QRect(410,340,80,30))

        self.comboBox1 = QComboBox(0,self,"comboBox1")
        self.comboBox1.setGeometry(QRect(310,10,120,31))
        self.comboBox1.setSizeLimit(12)
        self.comboBox1.setMaxCount(12)

        self.comboBox2 = QComboBox(0,self,"comboBox2")
        self.comboBox2.setGeometry(QRect(431,10,60,31))

        self.buttonGroup1 = QButtonGroup(self,"buttonGroup1")
        self.buttonGroup1.setGeometry(QRect(10,50,480,200))

        self.pushButton3 = QPushButton(self,"pushButton3")
        self.pushButton3.setGeometry(QRect(200,320,101,24))

        self.textLabel1 = QLabel(self,"textLabel1")
        self.textLabel1.setGeometry(QRect(71,10,160,30))

        self.languageChange()

        self.resize(QSize(504,380).expandedTo(self.minimumSizeHint()))
        self.clearWState(Qt.WState_Polished)

        self.connect(self.pushButton1,SIGNAL("clicked()"),self.close)
        self.connect(self.comboBox1,SIGNAL("activated(const
QString&)"),self.AddMonthsToCombo)
       
self.connect(self.pushButton3,SIGNAL("clicked()"),self.AddMonthsToCombo)


    def languageChange(self):
        self.setCaption(self.__tr("Form1"))
        self.pushButton1.setText(self.__tr("Beenden"))
        self.buttonGroup1.setTitle(QString.null)
        self.pushButton3.setText(self.__tr("pushButton3"))
        self.textLabel1.setText(self.__tr("textLabel1"))


    def AddMonthsToCombo(self):
                import time
                self.comboBox1.insertItem("Januar")
                self.comboBox1.insertItem("Februar")
                self.comboBox1.insertItem("MÀrz")
                self.comboBox1.insertItem("April")
                self.comboBox1.insertItem("Mai")
                self.comboBox1.insertItem("Juni")
                self.comboBox1.insertItem("Juli")
                self.comboBox1.insertItem("August")
                self.comboBox1.insertItem("September")
                self.comboBox1.insertItem("Oktober")
                self.comboBox1.insertItem("November")
                self.comboBox1.insertItem("Dezember")
                self.comboBox2.insertItem("2005")
                self.comboBox2.insertItem("2006")
                self.comboBox2.insertItem("2007")
                self.comboBox2.insertItem("2008")
                s = time.strftime("%B")
                t = time.strftime("%Y")
                self.comboBox1.setCurrentText(s)
                self.comboBox2.setCurrentText(t)
        

    def __tr(self,s,c = None):
        return qApp.translate("Form1",s,c)




this gets executed by this »wrapper«-script as shown to me in the tutorial:

mygui.py

from qt import *
from form1 import *
import time
import sys
if __name__ == "__main__":
    app = QApplication(sys.argv)
    f = Form1()
    f.show()
    app.setMainWidget(f)
    app.exec_loop()






More information about the Python-list mailing list