2014-04-16 197 views
0

我在使用python 2.7在tkinter中進行圖形用戶界面,這是我第一次,我似乎無法使代碼工作 - COMPARE按鈕不會做任何事情,它是假設顯示的東西label5。另外,某些進程在另一個文件中運行(數據加載&處理)。圖形用戶界面 - Tkinter

from Tkinter import * 
import ttk 
import tkMessageBox 

class GUI: 

    core = None 
    root = None 

    ctr = 0 

    def __init__(self, r): 
     self.root = r 
     self.root.title("") 
     self.root.protocol("WM_DELETE_WINDOW", self.quit) 
     self.root.minsize(550, 550) 

    def set_core(self, c): 
     # set the core object 
     self.core = c 

    def init(self): 
     # string assigned to label that gets changed 
     self.processing = StringVar() 
     self.processing.set(".") 

     # create label and assign above processing string, pack it 
     self.label = ttk.Label(self.root, textvariable=self.processing) 
     self.label.pack() 

     self.label2 = Label(self.root, text="", pady = 40) 
     self.label2.pack() 
     self.label3 = Label(self.root, text="Please enter ", pady = 5) 
     self.label3.pack() 


     self.pc = StringVar(None) 
     self.pCode = Entry(self.root,textvariable = self.pcode, width = 15) 
     self.pCode.pack() 
     self.key = self.pCode.get()   

     self.button1 = Button(self.root, text='COMPARE', width = 8, pady = 5, command = self.do_compare) 
     self.button1.pack() 


     self.var5 = StringVar() 
     self.label5 = Label(self.root, textvariable=self.var5, pady = 70) 
     self.var5.set("Result here...") 
     self.label5.pack() 

     self.button2 = Button(self.root, text='CLEAR ALL', width = 8, command = self.clear) 
     self.button2.pack() 

     self.button3 = Button(self.root, text='QUIT', width = 8, command = self.quit) 
     self.button3.pack() 


     # create button to start the core thread doing work, pack it 
     # self.button = ttk.Button(self.root, text="Do work", command=self.do_work) 
     # self.button.pack() 

     # create another button that can be clicked when doing work, pack it 
     # self.button_2 = ttk.Button(self.root, text=str(self.ctr), command=self.inc_ctr) 
     # self.button_2.pack() 

    def do_compare(self): 
     result = "" 
     if len(self.key) ==5 or len(self.key) 6: 
      result =self.do_work() 
      self.var5.set(result) 
     else: 
      result = 'Invalid Entry, please enter 5-6 alpha numeric characters... !'       
      self.var5.set(result) 

    def quit(self): 
     # our quit method to ensure we wait for the core thread to close first 
     if not self.core.processing: 
      self.core.running = False 
      self.root.quit() 

    # call back method for button 
    def do_work(self): 
     self.core.process_request() 

    # call back method for clear button 
    def clear(self): 
     self.var5.set("Result will be here...") 
     self.pc.set("") 

回答

1

do_compare()方法返回result,但回報是不會去任何地方。只需將標籤的textvariable設置爲變量result,它就會更新。在那裏你也不需要那個bind方法,那沒什麼用。這裏是我正在談論的一個例子:

root = Tk() 

def change_var():  # function to get 'result' 
    var.set('Result') # set var to 'result' 

var = StringVar()      # create var 
var.set('Empty Label')    # set var to value 
label = Label(root, textvariable=var) # make label to display var 
label.pack() 

Button(root, text='Change [var]', command=change_var).pack() 

root.mainloop() 
+0

非常感謝地質學家。該綁定是真的沒用。 'self.var5.set(self.process_request)' – user3267156

+0

我按照地圖學家的建議做了更改。但是,我得到這個錯誤 - TypeError:do_compare()只需要1個參數(給出2)。希望有人能夠在這方面給我啓發。 – user3267156

+0

這意味着你正在向'do_compare()'傳遞一個額外的參數。如果您想發佈一個新問題或對此問題進行編輯以包含修訂後的代碼,它將有助於查找錯誤的來源。 – atlasologist