我不知道在python中把mainloop函数放在哪里。当我使用这段代码时:
from tkinter import *
import sys
window = Tk()
def mainFunct():
while True:
label = Label(window,text="Hello World")
label2 = Label(window, text = "Hello World2")
menu = input("Please input something")
if menu == "a":
label.pack()
if menu == "b":
label2.pack()
if menu == "c":
sys.exit()
window.mainloop()
mainFunct()我希望标签在用户输入a时打包,当用户输入b时我希望label2打包。我不确定什么时候以及为什么要使用mainloop。现在当我运行程序时,只有在我输入了一些东西之后,GUi才会弹出,然后我甚至不能输入其他任何东西,我认为这与window.mainloop()函数有关,因为它只是一遍又一遍地循环,而不是再次运行while True循环。
发布于 2015-06-14 07:55:31
根据评论,我能够更好地理解您的问题。如果这就是你要找的,请告诉我:
import tkinter as tk
class HelloWorld(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="What's your input?", command=self.on_button)
self.button.pack()
self.entry.pack()
def on_button(self):
answer = self.entry.get()
if answer == "a":
print("Hello World")
elif answer == "b":
print("Hello World 2")
elif answer == "c":
root.destroy()
root = HelloWorld()
root.mainloop()因此,在处理用户的输入时,最好创建一个类并从中获取/比较信息。
现在,如果答案不是a、b或c,程序将不会响应,因此请进行相应的调整。
https://stackoverflow.com/questions/30824635
复制相似问题