简体   繁体   English

下面的代码运行,但是永远不会为我显示按钮。 我只想要一个简单的按钮说退出

[英]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.

相关问题 当我点击按钮时,按钮 id 应该返回给我。 JSON 文件 - When I click on the button, the button id should be returned to me. JSON file 当我按下退出按钮时 tkinter 不会退出,它只是改变了位置 - tkinter won't quit when I press quit button, it just changes location 尝试使用Selenium模块使我的代码单击Internet上的一个按钮,但是源代码对我来说很难阅读。 - Trying to use Selenium module to make my code to click one button on internet but source code is hard to read for me. 如何添加过滤器以便我可以对向经理报告的员工分组? 请指导我。 也让我知道下面的代码是否正确 - How to add filters so that I can group employees reporting to manager ?? Please guide me. Also let me know if the below code is correct or not 为什么这段代码没有画出我想要的开始按钮? - Why is this code not drawing the start button I want? 我想在 vscode 中使用 django 在服务器上显示一个简单的“你好”消息,但它说找不到页面 - I want to display a simple 'hello' message over the server using django in vscode but it says page not found 当我只想结束时,Python程序会继续打印“退出” - Python program keeps printing “quit” when I just want it to end 我想让一个 html 按钮将我重定向到 Flask 中的另一个端点 - I want to have an html button redirect me to another endpoint in flask 请为我解释以下代码行。 即使用数据框的 2 列创建熊猫系列 - Please explain the following line of code for me. i.e pandas series creation using 2 columns of a dataframe 如果我只想在条目下方添加一个简单的注释框,我应该使用Django的注释框架还是编写自己的注释框架? - If I just want a simple comment box below my entry, should I use Django's comment framework or write my own?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM