简体   繁体   English

pyqt4按钮单击事件打开新框架/窗口

[英]pyqt4 button click event open new frame/window

I am a newbie to Python GUI programming. 我是Python GUI编程的新手。 Currently I am trying to create buttons where once I clicked on it, it should open a new window/frame. 目前,我正在尝试创建按钮,在该按钮上单击后它将打开一个新窗口/框架。

This is my python GUI code: 这是我的python GUI代码:

class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        palette = QPalette()
        palette.setBrush(QPalette.Background,QBrush(QPixmap("blue-gradient-2.jpg")))
        self.setPalette(palette)

        self.setGeometry(300, 300, 600, 400)
        self.setWindowTitle("Testing Window")
        # self.setWindowIcon(QtGui.QIcon(''))
        self.home()


    def qr(self):
        backbtn = QtGui.QPushButton("Back" , self)
        backbtn.clicked.connect(home)
        backbtn.resize(100, 100)
        backbtn.move(100, 100)
        self.show()

    def home(self):
        btn = QtGui.QPushButton("QR Code", self)
        btn.clicked.connect(qr)

        btn.resize(100, 100)
        btn.move(100, 100)

        btn1 = QtGui.QPushButton("Face Recognition", self)
        btn1.clicked.connect(QtCore.QCoreApplication.instance().quit)

        btn1.resize(200, 100)
        btn1.move(300, 100)

        self.show()


def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

run()

Actually, this code I took from a tutorial. 实际上,这段代码是我从教程中获得的。 I saw even Tkinter can open a new window/frame. 我甚至看到Tkinter都可以打开新的窗口/框架。 But there isn't any tutorial on the PyQt opening a new window/frame. 但是PyQt上没有任何教程可以打开新的窗口/框架。

The error I am getting is: 我得到的错误是:

 Traceback (most recent call last):
  File "pyqt.py", line 47, in <module>
    run()
  File "pyqt.py", line 44, in run
    GUI = Window()
  File "pyqt.py", line 16, in __init__
    self.home()
  File "pyqt.py", line 28, in home
    btn.clicked.connect(qr)
NameError: global name 'qr' is not defined

I am sure there should be a appropriate way to deal with this error. 我确信应该有适当的方法来处理此错误。

Your problem is, that you haven't defined a global function qr() . 您的问题是,您尚未定义全局函数qr() But since you have defined a method with the same name, I suspect your intention was to do 但是由于您已经定义了一个具有相同名称的方法,所以我怀疑您的意图是

btn.clicked.connect(self.qr)

The same accounts for home() in your qr method 您的qr方法中的home()相同

backbtn.clicked.connect(self.home)

To open a new window with PyQt4 it is as easy as calling widget.show() on a widget without a parent. 要使用PyQt4打开新窗口,就像在没有父项的窗口小部件上调用widget.show()一样容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM