[英]The code below runs but will never display a button for me. I just want a simple button that says quit
I am trying to learn GUI on PyQt5 (which I have downloaded) and I want to create a button. 我正在尝试在PyQt5(已下载)上学习GUI,我想创建一个按钮。 After looking at a decent amount of articles the problem still persists.
在看了很多文章之后,问题仍然存在。
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(700,250,700,500)
self.setWindowTitle("Hang-Man")
self.show()
def home(self):
btn1 = QPushButton('Quit', self)
btn1.move(20,200)
self.show()
Well, I don't know what is your home
method, but the right way to do that is to define your UI widgets in the initUI
method, like that: 好吧,我不知道您的
home
方法是什么,但是正确的方法是在initUI
方法中定义UI小部件,如下所示:
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QWidget
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(700, 250, 700, 500)
self.setWindowTitle("Hang-Man")
btn1 = QPushButton('Quit', self)
btn1.move(20, 200)
btn1.clicked.connect(self.close)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Here, the "clicked" handler is connected to the close
method, to close the application. 在这里,“单击”处理程序连接到
close
方法,以关闭应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.