我有这个作为我的代码,大约中途是一个while循环,等待变量self.sub为1.这给了我问题,因为它将不再运行。它会说它的运行,但它不是。我会更具体,但我不知道如何。
from tkinter import *
root = Tk()
text_width = 100
def destroy_root():
try:
root.destroy()
except:
a = 0
class Application(Frame):
sub = 0
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
def submit(self):
if len(self.entry.get()) != 0:
self.text.config(width=text_width, state=NORMAL)
self.text.insert(END, '\n'+(str(self.entry.get())))
self.entry.delete(0, END)
self.text.config(width=text_width, state=DISABLED)
self.text.see(END)
self.sub = 1
def story(self):
self.text.config(state=NORMAL)
self.text.insert(END, '\nWelcome to the interactive, text-based game of Dimensia!\n\n_-_-_-_-_-_-_-_-_\n\n'
'Where reality is not what it seems, and parallels are more familiar than ever\n\n')
self.text.config(state=DISABLED)
self.sub = 0
condition = True
while condition:
if self.sub == 1:
condition = False
self.text.config(state=NORMAL)
self.text.insert(END, 'Hello')
self.text.config(state=DISABLED)
self.sub = 0
def createWidgets(self):
self.text = Text(self)
self.text.grid(row=0, column=0, columnspan=2, rowspan=2)
self.text.config(width=text_width, height=45, state=DISABLED)
self.border = Label(self)
self.border.grid(row=0, column=2, rowspan=2)
self.border.config(height=43, background='black')
instruction = 'Welcome to your personal scratchpad!!\n\n'
self.scratch_pad = Text(self)
self.scratch_pad.grid(row=1, column=3, columnspan=2, rowspan=2)
self.scratch_pad.config(width=50, height=30)
self.scratch_pad.insert(END, 'Scratch Pad\n_-_-_-_-_-_-_-_-_-_-_-_-_\n\n\n\n'+str(instruction))
self.QUIT = Button(self, command=self.quit)
self.QUIT.grid(row=2, column=0)
self.QUIT.config(width=1, text='Q')
self.entry = Entry(self)
self.entry.grid(row=2, column=1, columnspan=3)
self.entry.config(width=120)
self.submit_b = Button(self, text='Submit')
self.submit_b.config(command=self.submit)
self.submit_b.grid(row=2, column=4)
frame = Frame(self)
frame.grid(row=0, column=2, columnspan=2)
frame.config(height=1)
health_label = Label(frame, text='Health')
health_label.grid(row=0, column=0)
stamina_label = Label(frame, text='Stamina')
stamina_label.grid(row=2, column=0)
mana_label = Label(frame, text='Mana')
mana_label.grid(row=4, column=0)
courage_label = Label(frame, text='Courage')
courage_label.grid(row=6, column=0)
energy_label = Label(frame, text='Energy')
energy_label.grid(row=0, column=1)
strength_label = Label(frame, text='Strength')
strength_label.grid(row=2, column=1)
knowledge_label = Label(frame, text='Knowledge')
knowledge_label.grid(row=4, column=1)
confusion_label = Label(frame, text='Confusion')
confusion_label.grid(row=6, column=1)
inteligence_label = Label(frame, text='Inteligence')
inteligence_label.grid(row=0, column=2)
agility_label = Label(frame, text='Agility')
agility_label.grid(row=2, column=2)
hunger_label = Label(frame, text='Hunger')
hunger_label.grid(row=4, column=2)
coins_label = Label(frame, text='Coins')
coins_label.grid(row=6, column=2)
curiosity_label = Label(frame, text='Curiosity')
curiosity_label.grid(row=0, column=3)
fear_label = Label(frame, text='Fear')
fear_label.grid(row=2, column=3)
health_value = Spinbox(frame, text='Health', width=5)
health_value.grid(row=1, column=0)
health_value.config(from_=0, to_=100, state=DISABLED)
stamina_value = Spinbox(frame, text='Stamina', width=5)
stamina_value.grid(row=3, column=0)
stamina_value.config(from_=0, to_=100, state=DISABLED)
mana_value = Spinbox(frame, text='Mana', width=5)
mana_value.grid(row=5, column=0)
mana_value.config(from_=0, to_=100, state=DISABLED)
courage_value = Spinbox(frame, text='Courage', width=5)
courage_value.grid(row=7, column=0)
courage_value.config(from_=0, to_=100, state=DISABLED)
strength_value = Spinbox(frame, text='Strength', width=5)
strength_value.grid(row=3, column=1)
strength_value.config(from_=0, to_=100, state=DISABLED)
knowledge_value = Spinbox(frame, text='Knowledge', width=5)
knowledge_value.grid(row=5, column=1)
knowledge_value.config(from_=0, to_=100, state=DISABLED)
confusion_value = Spinbox(frame, text='Confusion', width=5)
confusion_value.grid(row=7, column=1)
confusion_value.config(from_=0, to_=100, state=DISABLED)
energy_value = Spinbox(frame, text='Energy', width=5)
energy_value.grid(row=1, column=1)
energy_value.config(from_=0, to_=100, state=DISABLED)
inteligence_value = Spinbox(frame, text='Inteligence', width=5)
inteligence_value.grid(row=1, column=2)
inteligence_value.config(from_=0, to_=100, state=DISABLED)
agility_value = Spinbox(frame, text='Agility', width=5)
agility_value.grid(row=3, column=2)
agility_value.config(from_=0, to_=100, state=DISABLED)
hunger_value = Spinbox(frame, text='Hunger', width=5)
hunger_value.grid(row=5, column=2)
hunger_value.config(from_=0, to_=100, state=DISABLED)
coins_value = Spinbox(frame, text='Coins', width=5)
coins_value.grid(row=7, column=2)
coins_value.config(from_=0, to_=100, state=DISABLED)
curiosity_value = Spinbox(frame, text='Curiosity', width=5)
curiosity_value.grid(row=1, column=3)
curiosity_value.config(from_=0, to_=100, state=DISABLED)
fear_value = Spinbox(frame, text='Fear', width=5)
fear_value.grid(row=3, column=3)
fear_value.config(from_=0, to_=100, state=DISABLED)
health_value.config(state=NORMAL)
health_value.config(value=100)
health_value.config(state=DISABLED)
stamina_value.config(state=NORMAL)
stamina_value.config(value=100)
stamina_value.config(state=DISABLED)
mana_value.config(state=NORMAL)
mana_value.config(value=0)
mana_value.config(state=DISABLED)
mana_label.config(state=DISABLED)
courage_value.config(state=NORMAL)
courage_value.config(value=50)
courage_value.config(state=DISABLED)
strength_value.config(state=NORMAL)
strength_value.config(value=35)
strength_value.config(state=DISABLED)
knowledge_value.config(state=NORMAL)
knowledge_value.config(value=5)
knowledge_value.config(state=DISABLED)
confusion_value.config(state=NORMAL)
confusion_value.config(value=75)
confusion_value.config(state=DISABLED)
energy_value.config(state=NORMAL)
energy_value.config(value=100)
energy_value.config(state=DISABLED)
inteligence_value.config(state=NORMAL)
inteligence_value.config(value=50)
inteligence_value.config(state=DISABLED)
agility_value.config(state=NORMAL)
agility_value.config(value=50)
agility_value.config(state=DISABLED)
hunger_value.config(state=NORMAL)
hunger_value.config(value=50)
hunger_value.config(state=DISABLED)
coins_value.config(state=NORMAL)
coins_value.config(value=150)
coins_value.config(state=DISABLED)
curiosity_value.config(state=NORMAL)
curiosity_value.config(value=50)
curiosity_value.config(state=DISABLED)
fear_value.config(state=NORMAL)
fear_value.config(value=25)
fear_value.config(state=DISABLED)
self.story()
a = 0
root.wm_title('Dimensia')
app = Application(master=root)
app.mainloop()
destroy_root()
答案 0 :(得分:2)
GUI已经处于等待条件的永久状态 - 这称为事件循环。所以,你不应该需要放入等待某事的循环。实际上,即使在while循环之前,您的代码仍在等待。它等待用户按下提交按钮,然后调用一个函数。
您需要删除while循环,只需在按钮回调中或在回调调用的函数中正确响应事件。