PyQt4 using global variable from function in class

2019-08-23 04:20发布

问题:

I understand that to use a global variable from a function, you Need to execute the function first:

def f():
    global s
    s = 'Hello'

f()
print(s)

But how do I use variable s globally in following example:

import sys
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QLineEdit, QLabel, QComboBox, QProgressBar, QFileDialog
from PyQt4.QtCore import QSize, pyqtSlot


class App(QMainWindow):

    def __init__(self):
        super(App, self).__init__()
        self.setGeometry(500, 300, 820, 350)
        self.setWindowTitle("Widget")
        self.initUI()

    def initUI(self):

        #Buttons
        btnposx = 30
        btnposy = 50

        self.btn4 = QPushButton('GetValue', self)
        self.btn4.move(btnposx,btnposy+220)
        self.btn4.clicked.connect(self.cb_get)

        self.cb = QComboBox(self)
        self.cb.move(btnposx+120,btnposy+150)
        self.cb.resize(80,22)
        self.cb.setMaximumSize(QSize(80,1000000))
        self.cb.addItem('A')
        self.cb.addItem('B')
        self.cb.addItem('C')
        self.cb.addItem('D')
        self.cb.addItem('E')

        self.show()

    @pyqtSlot()
    def cb_get(self):
        global s
        cbtext = str(self.cb.currentText())
        s = cbtext

print(s)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

This code shows a PyQt4 Widget. Function cb_get acquires the Value of a QcomboBox and can be used within the class App(). The Value is saved to variable s. How do I use variable s globally?

回答1:

The only way I seem to get it to work is to write a new function for s and execute it on button click:

import sys
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QLineEdit, QLabel, QComboBox, QProgressBar, QFileDialog
from PyQt4.QtCore import QSize, pyqtSlot


class App(QMainWindow):

    def __init__(self):
        super(App, self).__init__()
        self.setGeometry(500, 300, 820, 350)
        self.setWindowTitle("Widget")
        self.initUI()

    def initUI(self):

        #Buttons
        btnposx = 30
        btnposy = 50

        self.btn4 = QPushButton('GetValue', self)
        self.btn4.move(btnposx,btnposy+220)
        self.btn4.clicked.connect(self.cb_get)
        self.btn4.clicked.connect(self.p)

        self.cb = QComboBox(self)
        self.cb.move(btnposx+120,btnposy+150)
        self.cb.resize(80,22)
        self.cb.setMaximumSize(QSize(80,1000000))
        self.cb.addItem('A')
        self.cb.addItem('B')
        self.cb.addItem('C')
        self.cb.addItem('D')
        self.cb.addItem('E')

        self.show()

    @pyqtSlot()
    def cb_get(self):
        global s
        s = str(self.cb.currentText())

    def p(self):
        print(s)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

Well, at least now it has ist own function and I can write code for it seperately.



回答2:

def initUI(self):

    #Buttons
    btnposx = 30
    btnposy = 50

    self.btn4 = QPushButton('GetValue', self)
    self.btn4.move(btnposx,btnposy+220)
    self.btn4.clicked.connect(self.cb_get)

    self.cb = QComboBox(self)
    self.cb.move(btnposx+120,btnposy+150)
    self.cb.resize(80,22)
    self.cb.setMaximumSize(QSize(80,1000000))
    self.cb.addItem('A')
    self.cb.addItem('B')
    self.cb.addItem('C')
    self.cb.addItem('D')
    self.cb.addItem('E')
    self.s = None # initialize it here so you don't have to use global

    self.show()

@pyqtSlot()
def cb_get(self):
    cbtext = str(self.cb.currentText())
    self.s = cbtext

def get_s(self):
    return self.s

and

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    # print(ex.get_s) # this won't work since you have to click on btn4 first
    sys.exit(app.exec_())


标签: python pyqt4