python中的mainloop()函数
我不知道在python中把mainloop函数放在哪里。当我使用此代码时:python中的mainloop()函数,python,loops,tkinter,Python,Loops,Tkinter,我不知道在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
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时,我希望标签2被打包。我不确定何时以及为什么要使用mainloop。现在当我运行程序时,GUi只有在我输入了一些东西之后才会弹出,然后我甚至不能输入任何其他东西,我认为这与window.mainloop()有关函数,因为它只是一次又一次地循环,而不是再次运行while True循环。根据注释,我能够更好地理解您的问题。如果这是您要找的,请告诉我:
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
那么程序将不会有响应,因此请相应调整。感谢您的响应!,我有两种方法,我把mainloop()放进去,它会无限循环。。。因此,如果在mainFunct()之前有它,那么它只会显示屏幕,如果在mainFunct()之前有它,那么标签只会在我输入“c”后打包。例如,“code”mainFunct()root.mainloop()“code”GUI界面只会在mainFunction之后弹出,但当我想让mainFunction循环时,如何打包?谢谢,这正是我想要的!!。