我需要一些帮助来使我的GUI正常运行。因此对于我的GUI,我需要两个按钮和一个计时器。按下开始/暂停计时器倒数,然后再次按下暂停。重置其他按钮应该很容易,就像重置Qtimer一样。
我试图使用qtdesigner来确定gui的布局,并且在可行的同时,似乎无法在其中添加计时器,反之亦然。 (只是不会显示基于.py文件中哪一个位于较高位置)。对于计时器,我一直在弄混问题PyQt - showing countdown timer中答案的修改版本。任何帮助我获得与按钮接口一起使用的倒数计时器的帮助都将非常有帮助。
我对代码进行了一些更改,增加了固定按钮槽的位置,但仍然无法显示计时器,但出现错误“ Ui_Form”对象没有属性“ setCentralWidget”,这对我来说是一生无法解决的。请有人帮我在gui上获得计时器,我可以配置计时器的后退位置。
import sys
from PyQt5 import QtCore, QtWidgets
#import pymongo
#import socket
#import json
#from time import sleep
DURATION_INT = 300
state = "stopped"
#connection = pymongo.MongoClient(host='192.168.4.2', port=27017)
#db = connection.restdb
class Ui_Form(object):
def setupUi(self, Form):
global state
super().__init__()
Form.setObjectName("Form")
Form.resize(648, 561)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(170, 460, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(190, 110, 231, 101))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(230, 270, 61, 51))
self.lineEdit_2.setObjectName("lineEdit_2")
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(290, 270, 21, 51))
self.textEdit.setObjectName("textEdit")
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(310, 270, 61, 51))
self.lineEdit_3.setObjectName("lineEdit_3")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(360, 460, 93, 28))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
self.pushButton.clicked.connect(self.start_pause)
self.pushButton_2.clicked.connect(self.reset)
######################################################
self.time_left_int = DURATION_INT
self.widget_counter_int = 0
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
vbox = QtWidgets.QVBoxLayout()
central_widget.setLayout(vbox)
self.pages_qsw = QtWidgets.QStackedWidget()
vbox.addWidget(self.pages_qsw)
self.time_passed_qll = QtWidgets.QLabel()
vbox.addWidget(self.time_passed_qll)
self.widget_one = QtWidgets.QLabel()
self.pages_qsw.addWidget(self.widget_one)
self.timer_start()
self.update_gui()
#####################################################
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton_2.setText(_translate("Form", "Reset"))
self.pushButton.setText(_translate("Form", "Start/Pause"))
def timer_start(self):
self.time_left_int = DURATION_INT
self.my_qtimer = QtCore.QTimer()
self.my_qtimer.timeout.connect(self.timer_timeout)
self.my_qtimer.start(1000)
self.update_gui()
def timer_timeout(self):
self.time_left_int -= 1
if self.time_left_int == -1:
self.widget_counter_int = (self.widget_counter_int + 1) % 4
self.pages_qsw.setCurrentIndex(self.widget_counter_int)
self.time_left_int = DURATION_INT
self.update_gui()
def update_gui(self):
self.time_passed_qll.setText(str(self.time_left_int))
def start_pause(self):
global state
if (state == "stopped" or state == "paused"):
state = "running"
print(state)
#for g in gameData.find():
# gameData.update({'_id' : g['_id']}, {'state': state})
elif (state == "running"):
state = "paused"
print(state)
#for g in gameData.find():
# gameData.update({'_id' : g['_id']}, {'state': state})
def reset(self):
global state
state = "stopped"
print(state)
#for g in gameData.find():
# gameData.update({'_id' : g['_id']}, {'state': state})
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QMainWindow()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
TL; DR:您似乎没有完全掌握GUI对象树(哪些QWidget项连接到哪个QWidget /布局父级)
更改
Form.setCentralWidget(central_widget)
到
self.setCentralWidget(central_widget)
将显示该表格,带有倒数计时器的标签位于左下角。它对按钮没有反应,因为带有标签和widget_one的QVBoxLayout位于顶部,但至少它倒计时。
您可以使用以下语句向QWidget(或子类)添加背景色:
pal = QtGui.QPalette()
pal.setColor(QtGui.QPalette.Background, QtCore.Qt.red)
self.widget_one.setAutoFillBackground(True)
您将看到手动放置的小部件随后隐藏在红色矩形的后面。这也意味着 widget_one 将捕获所有鼠标事件,并且您无法单击任何东西。
我建议您不要混合使用setLayout()和setCentralWidget(),因为这将导致(有时不明显)鼠标和键盘事件。
总而言之,我想您想显示这样的内容(我已经将QStackedWidget留在其中了,因为我假设您稍后要使用它)?
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
#import pymongo
#import socket
#import json
#from time import sleep
DURATION_INT = 300
state = "stopped"
#connection = pymongo.MongoClient(host='192.168.4.2', port=27017)
#db = connection.restdb
class Ui_Form(object):
def setupUi(self, Form):
global state
super().__init__()
Form.setObjectName("Form")
Form.resize(648, 561)
######################################################
self.time_left_int = DURATION_INT
self.widget_counter_int = 0
central_widget = QtWidgets.QWidget()
Form.setCentralWidget(central_widget)
vbox = QtWidgets.QVBoxLayout()
central_widget.setLayout(vbox)
self.time_passed_qll = QtWidgets.QLabel()
vbox.addWidget(self.time_passed_qll)
self.time_passed_qll.setAlignment(QtCore.Qt.AlignCenter)
self.time_passed_qll.setFrameStyle(QtWidgets.QFrame.Panel | QtWidgets.QFrame.Sunken);
self.time_passed_qll.setTextFormat(QtCore.Qt.RichText) # to be able to use html tags for color!
self.pages_qsw = QtWidgets.QStackedWidget()
vbox.addWidget(self.pages_qsw)
self.widget_one = QtWidgets.QWidget()
#pal = QtGui.QPalette()
#pal.setColor(QtGui.QPalette.Background, QtCore.Qt.red)
#self.widget_one.setAutoFillBackground(True)
#self.widget_one.setPalette(pal)
self.pages_qsw.addWidget(self.widget_one)
self.pushButton_2 = QtWidgets.QPushButton(self.widget_one)
self.pushButton_2.setGeometry(QtCore.QRect(170, 460, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.lineEdit = QtWidgets.QLineEdit(self.widget_one)
self.lineEdit.setGeometry(QtCore.QRect(190, 110, 231, 101))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.widget_one)
self.lineEdit_2.setGeometry(QtCore.QRect(230, 270, 61, 51))
self.lineEdit_2.setObjectName("lineEdit_2")
self.textEdit = QtWidgets.QTextEdit(self.widget_one)
self.textEdit.setGeometry(QtCore.QRect(290, 270, 21, 51))
self.textEdit.setObjectName("textEdit")
self.lineEdit_3 = QtWidgets.QLineEdit(self.widget_one)
self.lineEdit_3.setGeometry(QtCore.QRect(310, 270, 61, 51))
self.lineEdit_3.setObjectName("lineEdit_3")
self.pushButton = QtWidgets.QPushButton(self.widget_one)
self.pushButton.setGeometry(QtCore.QRect(360, 460, 93, 28))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
self.pushButton.clicked.connect(self.start_pause)
self.pushButton_2.clicked.connect(self.reset)
self.timer_start()
self.update_gui()
#####################################################
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton_2.setText(_translate("Form", "Reset"))
self.pushButton.setText(_translate("Form", "Start/Pause"))
def timer_start(self):
self.time_left_int = DURATION_INT
self.my_qtimer = QtCore.QTimer()
self.my_qtimer.timeout.connect(self.timer_timeout)
self.my_qtimer.start(1000)
self.update_gui()
def timer_timeout(self):
global state
if state == "running":
self.time_left_int -= 1
if self.time_left_int == -1:
self.widget_counter_int = (self.widget_counter_int + 1) % 4
self.pages_qsw.setCurrentIndex(self.widget_counter_int)
self.time_left_int = DURATION_INT
self.update_gui()
def update_gui(self):
self.time_passed_qll.setText('<p style="color:green;font-size:24px">{}</p>'.format(self.time_left_int))
self.lineEdit.setText(str(self.time_left_int))
def start_pause(self):
global state
if (state == "stopped" or state == "paused"):
state = "running"
print(state)
#for g in gameData.find():
# gameData.update({'_id' : g['_id']}, {'state': state})
elif (state == "running"):
state = "paused"
print(state)
#for g in gameData.find():
# gameData.update({'_id' : g['_id']}, {'state': state})
def reset(self):
global state
self.time_left_int = DURATION_INT
state = "stopped"
print(state)
#for g in gameData.find():
# gameData.update({'_id' : g['_id']}, {'state': state})
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QMainWindow()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())