gpt4 book ai didi

python - root.after 后 TKinter GUI 卡住

转载 作者:行者123 更新时间:2023-12-01 01:56:28 24 4
gpt4 key购买 nike

我正在尝试创建代码,以在输入持续时间范围内逐步增加直流电源上的电压。我已经设置了一个 GUI 来执行此操作(这是我第一次尝试制作 GUI,如果代码很奇怪,抱歉),并且一切正常......除了 GUI 在代码执行时卡住,所以我无法停止环形。我已经研究了几个小时并学会了使用 root.after 而不是 time.sleep,但它似乎对 HeatLoop 功能没有帮助。 GUI 现在更新,但只是偶尔更新,当我将鼠标悬停在 GUI 上时,仍然会出现“等待光标”。有什么办法可以解决这个问题吗?

我修改了下面使用的代码,因此它应该可以在任何计算机上运行,​​而无需进行编辑。

import datetime
import time
from tkinter import *

class GUIClass:


def __init__(self, root):
"""Initialize the GUI"""

self.root = root
self.percent = StringVar()
self.percent.set("00.00 %")
self.error = StringVar()
self.STOP = False
self.error.set("---")
self.currentvoltage = StringVar()
self.currentvoltage.set("Current Voltage: 00.00 V")
self.DT = datetime.datetime

# Create and attach labels
label1 = Label(root, text='Voltage')
label2 = Label(root, text='Ramp Duration')
label3 = Label(root, text='Percent Done: ')
label4 = Label(root, textvariable=self.percent)
label5 = Label(root, text="Error Message: ")
label6 = Label(root, textvariable=self.error)
label7 = Label(root, textvariable=self.currentvoltage)
label1.grid(row=0, column=0, sticky=W)
label2.grid(row=1, column=0, sticky=W)
label3.grid(row=2, column=0, sticky=W)
label4.grid(row=2, column=1, sticky=W)
label5.grid(row=3, column=0, sticky=W)
label6.grid(row=3, column=1, sticky=W)
label7.grid(row=3, column=2, sticky=E)



# Create and attach entries
self.voltage = Entry(root)
self.duration = Entry(root)
self.voltage.grid(row=0, column=1)
self.duration.grid(row=1, column=1)

# Create, bind, and attach buttons
HeatButton = Button(root, text='Heat')
HeatButton.bind("<Button-1>", self.Heat)
HeatButton.grid(row=0, column=2)

CoolButton = Button(root, text='Cool')
CoolButton.bind("<Button-1>", self.Heat)
CoolButton.grid(row=1, column=2)

StopButton = Button(root, text='Stop')
StopButton.bind("<Button-1>", self.Stop)
StopButton.grid(row=2, column=2)


def HeatLoop(self, condition, TimeStart, TimeDuration, MaximumVoltage, Fraction=0):
"""Heat up the cell while the condition is true"""
if condition:
self.percent.set("{:2.2f}%".format(Fraction * 100))
print(MaximumVoltage)
self.currentvoltage.set("Current Voltage: {:2.2f} V".format(Fraction*MaximumVoltage))
self.Update()
CurrentTime = self.DT.now()
ElapsedTime = (CurrentTime.second/3600 + CurrentTime.minute/60 + CurrentTime.hour
- TimeStart.second/3600 - TimeStart.minute/60 - TimeStart.hour)
Fraction = ElapsedTime / TimeDuration
print(Fraction)
self.root.after(5000)
self.HeatLoop(bool(not self.STOP and Fraction < 1),
TimeStart, TimeDuration, MaximumVoltage, Fraction)


# Define function to heat up cell
def Heat(self, event):
# Initialize Parameters
self.STOP = False
self.error.set("---")
self.Update()



# Try to get voltage and duration from the GUI
MaxVoltage = self.voltage.get()
TimeDuration = self.duration.get()
try:
MaxVoltage = float(MaxVoltage)
try:
TimeDuration = float(TimeDuration)
except:
self.error.set("Please enter a valid time duration")
self.Update()
self.STOP = True
except:
self.error.set("Please enter a valid voltage value")
self.Update()
self.STOP = True

TimeStart = self.DT.now()

self.HeatLoop(True,
TimeStart, TimeDuration, MaxVoltage)


def Stop(self, event):
self.STOP = True
print("turned off voltage")

def Update(self):
self.root.update_idletasks()
self.root.update()


root1 = Tk()
a = GUIClass(root1)
root1.mainloop()

最佳答案

root.after(5000)time.sleep(5) 没有什么不同。它完全按照您的指示执行:卡住五秒钟。

如果你想每五秒运行一次self.HeatLoop,方法如下:

self.root.after(5000, self.HeatLoop, 
bool(not self.STOP and Fraction < 1),
TimeStart, TimeDuration, MaximumVoltage,
Fraction)

当您向 after 提供两个或多个参数时,tkinter 会将该函数添加到队列中,并在时间到期后调用该函数。这允许事件循环在五秒间隔内继续处理事件。

更好的编写方法是检查函数内部的条件,而不是将条件传入,以便在执行工作之前立即评估条件,而不是在执行工作前五秒。

例如:

def HeatLoop(self, TimeStart, TimeDuration, MaximumVoltage, Fraction=0):
if self.STOP and Fraction < 0:
return
...
self.root.after(5000, self.HeatLoop,
TimeStart, TimeDuration, MaximumVoltage,
Fraction)

关于python - root.after 后 TKinter GUI 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50123268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com