PyQt4使用类

时间:2018-04-16 10:34:15

标签: python pyqt4

我理解要使用函数中的全局变量,需要先执行函数:

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

f()
print(s)

但是如何在以下示例中全局使用变量s

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_())

此代码显示了PyQt4 Widget。函数cb_get获取QcomboBox的值,可以使用withinApp()。值将保存到变量s。如何全局使用变量s

2 个答案:

答案 0 :(得分:0)

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

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_())

答案 1 :(得分:0)

我似乎唯一能让它工作的方法是为s编写一个新函数并在按钮点击时执行它:

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_())

好吧,至少现在它有自己的功能,我可以单独编写代码。