2014-02-16 57 views
1

我有這個作爲我的代碼,大約一半是一個while循環等待,直到變量self.sub是1.這是給我的問題,因爲它將不再運行。它會說它的運行,但它不是。我會更具體,但我不知道如何。在tkinter模塊中,我將如何使GUI等待,直到輸入已註冊

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() 
+1

請閱讀此:http://stackoverflow.com/help/mcve – jonrsharpe

回答

2

GUI已處於等待條件的永久狀態 - 這被稱爲事件循環。所以,你不應該永遠需要放入你自己的等待某件事的循環。事實上,你的代碼,即使在你的while循環之前。它等到用戶按下提交按鈕,然後調用一個函數。

您需要刪除您的while循環,並在您的按鈕回調或您的回調調用的函數中適當地響應該事件。