Accessing Widgets from ui.py module



  • I'm not sure why im going brain dead on this. I've tried for two days and it should be dirt simple but I think I an not getting something about class and variable visibility. Per every tutorial out there i want to keep the gui seperate from the main code. So following example I created hello world ui.
    @

    from PyQt4 import QtCore, QtGui

    try:
    _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
    def _fromUtf8(s):
    return s

    try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
    def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig)

    class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(640, 480)
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.label1 = QtGui.QLabel(self.centralwidget)
    self.label1.setGeometry(QtCore.QRect(190, 210, 251, 111))
    font = QtGui.QFont()
    font.setPointSize(24)
    self.label1.setFont(font)
    self.label1.setObjectName(_fromUtf8("label1"))
    self.lcd1 = QtGui.QLCDNumber(self.centralwidget)
    self.lcd1.setGeometry(QtCore.QRect(210, 30, 241, 111))
    self.lcd1.setObjectName(_fromUtf8("lcd1"))
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtGui.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
    self.menubar.setObjectName(_fromUtf8("menubar"))
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtGui.QStatusBar(MainWindow)
    self.statusbar.setObjectName(_fromUtf8("statusbar"))
    MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.label1.setText(_translate("MainWindow", "Hello World", None))
    

    ===========================================================

    And Main file as:

    #!/usr/bin/env python
    import sys
    from helloworld import *
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
    ==== all the below and same with label fail saying lcd1 or label1 are not members of (whatever i prepended)
    lcd1.display='999'
    ui.lcd1.display='999'
    Ui_MainWindow.lcd1.display = '999'

    ===============================================
    @

    and that runs file. However, I would like to change, say the value of the lcd or label now.
    and all attempts at referencing them say they are not part of ?
    I've left the attempts above as comments.
    Also, and this is silly, would code go after sys.exec or after mainwindow.show or???

    Im fluent in a lot of languages but for some reason im not understanding this.
    Thanks for any guidance.
    p.s. Comments in code '#' got turned into numbered lines so i took them out. am i posting wrong?

    [edit: added missing coding tags @ SGaist]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.