如何使按钮成为可点击的,以便更改值并继续其他程序。
我正在尝试制作一个简单的GUI,我想通过标签小部件将值从1打印到100.另一个要求是当循环中的值等于4的倍数时(each_item%== 0)我想通过pushButtonChange将其翻倍并打印到标签上。我已经为其编写了代码,但是程序不等待通过单击pushButtonChange来更改值。如何使程序停止并等待单击按钮并继续。
import sys
from PyQt5.QtWidgets import QDialog,QApplication
from verifygui import *
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui=Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButtonStart.clicked.connect(self.start)
self.show()
def start(self):
list1=list(range(100))
# print(list1)
for each_item in list1:
if each_item%4==0:
self.ui.pushButtonChange.clicked.connect(lambda:self.valueChanged(each_item))
else:
self.ui.label.setText(str(each_item))
def valueChanged(self,each_item):
new_value=each_item*2
self.ui.label.setText(str(new_value))
if __name__=="__main__":
app=QApplication(sys.argv)
w=MyForm()
w.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
我仍然不确定我是否理解问题,但是我输入了代码。
按钮Start
在标签中设置第一个值或在控制台“已经运行”中打印
按钮Change
将+1加到当前值,如果值除以4,则重复* 2。
如果之前未按下Not running
,它将在控制台Start
中打印,否则wen值将为100(它将停止它,您必须再次使用Start
)
它不使用for
循环,因为仅在按下按钮时才更改值。
import sys
from PyQt5.QtWidgets import QDialog, QApplication, QLabel, QPushButton, QVBoxLayout
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout(self)
self.label = QLabel(self, text="?")
self.layout.addWidget(self.label)
self.buttonStart = QPushButton(self, text="Start")
self.buttonStart.clicked.connect(self.start)
self.layout.addWidget(self.buttonStart)
self.buttonChange = QPushButton(self, text="Change")
self.buttonChange.clicked.connect(self.valueChanged)
self.layout.addWidget(self.buttonChange)
self.is_running = False
self.current_item = 0
self.show()
def start(self):
if not self.is_running:
self.is_running = True
self.current_item = 1
self.label.setText(str(self.current_item))
else:
print("Already running")
def valueChanged(self):
if self.is_running:
self.current_item += 1
if self.current_item % 4 == 0:
self.label.setText(str(self.current_item * 2) + " <-- double")
else:
self.label.setText(str(self.current_item))
if self.current_item >= 100:
self.is_running = False
else:
print("Not running")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyForm()
window.show()
sys.exit(app.exec_())
这是我的第一个版本。它使用定时器每0.5毫秒(500毫秒)更改标签中的值。
当您按Start
时,它开始计数。当您按Change
时,将值除以4,然后乘以* 2
import sys
from PyQt5.QtWidgets import QDialog, QApplication, QLabel, QPushButton, QVBoxLayout
from PyQt5.QtCore import QTimer
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout(self)
self.label = QLabel(self, text="?")
self.layout.addWidget(self.label)
self.buttonStart = QPushButton(self, text="Start")
self.buttonStart.clicked.connect(self.start)
self.layout.addWidget(self.buttonStart)
self.buttonChange = QPushButton(self, text="Change")
self.buttonChange.clicked.connect(self.valueChanged)
self.layout.addWidget(self.buttonChange)
self.current_item = 0
self.show()
def start(self):
self.current_item = 0
self.timer = QTimer()
self.timer.timeout.connect(self.valueUpdate)
self.timer.start(500) # 500ms = 0.5s
def valueUpdate(self):
self.current_item += 1
self.label.setText(str(self.current_item))
if self.current_item >= 100:
self.timer.stop()
def valueChanged(self):
if self.current_item is not None:
if self.current_item % 4 == 0:
new_value = self.current_item * 2
self.label.setText(str(new_value))
else:
print("Not running")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyForm()
window.show()
sys.exit(app.exec_())