我的 Tkinter pop_up 方法似乎按预期自行运行,并放置在调用它的方法中的替代行中。但是当放置在需要的地方时,弹出窗口不会显示。它可以看到运行时带有调试打印输出,但不弹出。
` def pop_up(self, instruction):
root = Tk()
root.geometry("500x250")
root.title("Cheese Time")
def button_command():
global instruction_exe_time
t = strptime(entry.get()) # timeobject
instruction_exe_time= mktime(t) # time in seconds
self.set_time(instruction[0],instruction_exe_time)
print(ctime(instruction_exe_time))
root.destroy()
label_instruction = Label(root, text = instruction[1][0], font=('Helvetica 12 bold'))
label_instruction.pack(pady=20)
label = Label(root, text = "Enter time of execution:", font= ('Helvetica 10 bold'))
label.pack(pady= 10)
entry = Entry(root, width = 22, font=('Helvetica 14 bold'))
entry.insert(10,f"{ctime()}")
entry.pack(pady=10)
button = Button(root, text= "OK", command=lambda:button_command(), font= ('Helvetica 14 bold'))
button.pack(pady=20, padx=20)
root.mainloop()`
# @threaded
def make_cheese(self):
print("Press enter to continue or 'A' to enter time of execution")
for instruc in self.instructions.items():
# self.pop_up(instruc) pop_runs when placed here also makes other pop_up calls run
if instruc[1][1] == True:
self.sleep_adjust(instruc[0])
#sleep(10) #instruc[1][2]
print(instruc[0], ": ", instruc[1][0])
self.pop_up(instruc) # does not work here
else:
# self.pop_up(instruc) pop_up also runs properly when placed here and makes other pop_ups run
print(instruc[0], ": ",instruc[1][0])
input1 = input()
if input1 == "A" or input1 == "a":
self.pop_up(instruc) # does not work here
else:
self.set_time(instruc[0],time())
self.set_done(instruc[0])
将 pop_up 方法放置在调用它的方法的替代部分中时,运行它,然后在需要的地方正确运行其他调用。
是什么阻止了它的显示?
在这里,不要创建
Tk()
的多个实例,因为它只能创建一次。相反,要创建多个窗口,请使用 Toplevel()
创建实例。
希望有帮助!