Python PyQt5-如何启用在运行单击的按钮后无法显示的新(第三)按钮?
下面是一个简单的程序,用于在单击前两个按钮中的任何一个时创建两个按钮和第三个按钮。但它不起作用。当我单击前两个按钮中的一个时,控制台中会显示一条消息,指示已单击该按钮,但没有显示第三个按钮。为什么?谢谢Python PyQt5-如何启用在运行单击的按钮后无法显示的新(第三)按钮?,python,pyqt5,qpushbutton,Python,Pyqt5,Qpushbutton,下面是一个简单的程序,用于在单击前两个按钮中的任何一个时创建两个按钮和第三个按钮。但它不起作用。当我单击前两个按钮中的一个时,控制台中会显示一条消息,指示已单击该按钮,但没有显示第三个按钮。为什么?谢谢 from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
def setup_pushButton(self, word, x, y, width, height):
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(x, y, width, height))
self.pushButton.setText(word)
self.pushButton.clicked.connect(self.but_clicked)
def create_pushButtons(self):
self.setup_pushButton('apple', 100, 110, 75, 23)
self.setup_pushButton('car', 20, 110, 75, 23)
def but_clicked(self):
print('clicked')
self.setup_pushButton('house', 250, 110, 75, 23)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
ui.create_pushButtons()
Form.show()
sys.exit(app.exec_())
下面带有内联注释的代码将帮助您找出哪里出了问题,以及我为什么要把东西放在哪里。最重要的是。。。您的按钮名称可能会不时更改,因此我使用
setattr
和getattr
为您创建了代码变量。之后要做的工作更少,更注重面向对象的Python;-)
注意:按照惯例,按钮的初始化通常发生在setupUI
中。在您的情况下,我们将按钮视为主setupUI
的附加功能,之前未考虑,因为它是您心爱的产品发布客户要求的虚拟产品中的附加功能
享受吧
Tributton示例:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(QtWidgets.QWidget, object):
def __init__(self, parent=None):
print '> This is start of main app'
super(Ui_Form, self).__init__(parent)
self.setupUI(self)
self.buttonlist = [('apple', 100, 110, 75, 23),
('car', 20, 110, 75, 23),
('house', 250, 110, 75, 23)]
self.create_pushButtons()
print '> Everything is painted... lets show it on screen.'
def setupUI(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Twobutton Example", "Twobutton Example"))
def create_pushButtons(self):
for item in self.buttonlist:
# set attribute with variable name (e.g. "car" may change into "cars")
setattr(self, 'pushButton_%s' % (item[0]), QtWidgets.QPushButton(self))
# painting the variale named button.
# x, y, width, height
getattr(self, 'pushButton_%s' % (item[0])).setGeometry(QtCore.QRect(item[1], item[2], item[3], item[4]))
getattr(self, 'pushButton_%s' % (item[0])).setText(item[0])
# create the connection aswell for the variable named button in a single "go".
if item[0] in [self.buttonlist[0][0], self.buttonlist[1][0]]:
getattr(self, 'pushButton_%s' % (item[0])).clicked.connect(self.buton_clicked)
if item[0] == self.buttonlist[2][0]:
getattr(self, 'pushButton_%s' % (item[0])).clicked.connect(self.house_but_clicked)
getattr(self, 'pushButton_%s' % (item[0])).hide()
def buton_clicked(self):
#"Murder she wrote".. : who done it... was it car or apple... where did the order came from?
sender = self.sender()
text = sender.text()
print('The button %s was clicked' % text)
# print 'clicked' to python standard out. In an editor like komodo edit you see imidiatly what you do.
sys.stdout.flush()
# car button:
# if button house is hidden... lets show it (again).
if text in [self.buttonlist[1][0], ]:
getattr(self, 'pushButton_%s' % (self.buttonlist[2][0])).show()
# apple button:
# if button house is show... lets hide it again.
if text in [self.buttonlist[0][0], ]:
getattr(self, 'pushButton_%s' % (self.buttonlist[2][0])).hide()
def house_but_clicked(self):
sender = self.sender()
print('The button %s was clicked and its going to be connected to something else' % sender.text())
sys.stdout.flush()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = Ui_Form()
ui.show()
sys.stdout.flush()
sys.exit(app.exec_())
编辑:以显示12月10日评论OP后变量objectname的正确使用
操作脚本:
snippet...
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
t = ui
ui.create_pushButtons()
Form.show()
# print dir(__builtins__)
# l = dir(Ui_Form.setup_pushButton)
# from pprint import pprint
# pprint(l)
# t = Ui_Form()
variables = [i for i in dir(t) if not inspect.ismethod(i)]
print variables
sys.exit(app.exec_())
snippet...
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = Ui_Form()
t = ui
ui.show()
sys.stdout.flush()
variables = [i for i in dir(t) if not inspect.ismethod(i)]
print variables
sys.exit(app.exec_())
输出打印变量:
[“class”、“delattr”、“dict”、“doc”、“format”、“getattribute”、“hash”、“init”、“模块”、“new”、“reducereduce”、“reduce ex、、repr>”setattr”、“sizeof”、“str”、“子类挂钩”、“weakref”、“但单击”、“创建按钮”、“按钮1”、“设置UI”、“设置按钮”]
Tributton示例脚本:
snippet...
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
t = ui
ui.create_pushButtons()
Form.show()
# print dir(__builtins__)
# l = dir(Ui_Form.setup_pushButton)
# from pprint import pprint
# pprint(l)
# t = Ui_Form()
variables = [i for i in dir(t) if not inspect.ismethod(i)]
print variables
sys.exit(app.exec_())
snippet...
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = Ui_Form()
t = ui
ui.show()
sys.stdout.flush()
variables = [i for i in dir(t) if not inspect.ismethod(i)]
print variables
sys.exit(app.exec_())
输出打印变量:
这是主应用程序的开始
一切都画好了…让我们在屏幕上展示吧。
['DrawChildren','DrawWindowBackground','IgnoreMask','PaintDeviceMetric',
...
‘pos’、‘previousInFocusChain’、‘property’、‘Putton_apple’、‘Putton_car’、‘Putton_house’、‘pyqtConfigure’、‘raise_’、‘receivers’、‘rect’、‘releaseKeyboard’,
...
“windowOpacity”、“windowRole”、“WindowsState”、“windowTitle”、“windowTitleChanged”、“windowType”、“x”、“y”]
正如您所看到的,有三个独立的中庭,它们有自己的objectName空间,即……'Putton\u apple'、'Putton\u car'和'Putton\u house'实际上,我的代码生成了新的按钮,但它没有显示出来。通过添加--self.putton.show()--它现在可以正常工作了。下面是完整的修订代码
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
def setup_pushButton(self, word, x, y, width, height):
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(x, y, width, height))
self.pushButton.setText(word)
self.pushButton.clicked.connect(self.but_clicked)
def create_pushButtons(self):
self.setup_pushButton('apple', 100, 110, 75, 23)
self.setup_pushButton('car', 20, 110, 75, 23)
def but_clicked(self):
print('clicked')
self.setup_pushButton('house', 250, 110, 75, 23)
self.pushButton.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
ui.create_pushButtons()
Form.show()
sys.exit(app.exec_())
正如您在我的解决方案中所看到的,您必须首先创建所有三个按钮。然后单击时隐藏一个按钮并在命令上显示。可以使用功能性if语句交换隐藏/显示功能,在该语句下,开始条件为car=0&apple=1,单击任一按钮时切换,结果为:car=1&apple=0。请选择answer如果这是首选解决方案。谢谢。实际上,我找到了一种方法来做我想做的事情。创建第三个按钮时,它没有显示出来。通过在代码中添加行,所有按钮都使用相同的
objectName
空格(变量)这是一种本质上不正确的脚本编写方式!每个按钮都应该有自己的变量objectName
空格,以便可以引用它并修改其行为或外观。。