2013-10-30 35 views
0

我對python很陌生。如何使tkinter對話框首先出現?

我編碼如下。 當用戶點擊「單擊按鈕來設置CPE」。按鈕,將出現對話窗口並顯示客戶列表。

我的問題是,當用戶點擊「單擊按鈕來設置CPE」。按鈕,Listing()功能首先工作。那時主窗口已經死了。完成Listing()後,將出現對話框。

怎樣才能使對話框首先出現,並在出現對話框後顯示信息。

import Tkinter 

class myWindow: 
    addr = '' 
    def __init__(self): 

     self.mw = Tkinter.Tk() 
     self.mw.option_add("*font", ("Arial", 15, "normal")) 
     self.mw.geometry("+250+200") 
     self.mw.title("Example of Custom Dialog-Window") 

     # CPE 
     self.btn_cpe = Tkinter.Button(self.mw, text = "Click button to setup CPE.", command = self.btnCPE) 
     self.btn_cpe.pack(padx = 20, pady = 20) 
     self.mw.mainloop() 


    def btnCPE(self): 

     self.dialogwindow = Tkinter.Toplevel() 
     self.dialogwindow.title("Dialog Window") 
     self.dialogwindow.geometry("+500+350") 
     self.dialogwindow.maxsize(500, 350) 
     self.dialogwindow.minsize(500, 350) 

     self.lab1 = Tkinter.Label(self.dialogwindow, text = "Checking info") 
     self.lab1.pack() 

     self.lab_addr = Tkinter.Label(self.dialogwindow, text = "Address : ") 
     self.lab_addr.pack() 

     # Refresh 
     self.btn_refresh = Tkinter.Button(self.dialogwindow, text = "Refresh", command = self.refresh) 
     self.btn_refresh.pack() 

     self.btn_cpe_exit = Tkinter.Button(self.dialogwindow, text = "Exit", command = self.dialogwindow.destroy) 
     self.btn_cpe_exit.pack() 

     # This is the important line: It tells the main-window to lock: 
     self.dialogwindow.grab_set() 

     self.Listing() 
     self.lab_addr['text'] = "address : " + self.addr; 

    def Listing(self): 
     # access the db and set the address into self.addr 

回答

0

試試這個:

import Tkinter 

class myWindow: 
    addr = '' 
    def __init__(self): 
     # CPE 
     self.btn_cpe = Tkinter.Button(mw, text = "Click button to setup CPE.", command = self.btnCPE) 
     self.btn_cpe.pack(padx = 20, pady = 20) 

    def btnCPE(self): 

     self.dialogwindow = Tkinter.Toplevel() 
     self.dialogwindow.title("Dialog Window") 
     self.dialogwindow.geometry("+500+350") 
     self.dialogwindow.maxsize(500, 350) 
     self.dialogwindow.minsize(500, 350) 

     self.lab1 = Tkinter.Label(self.dialogwindow, text = "Checking info") 
     self.lab1.pack() 

     self.lab_addr = Tkinter.Label(self.dialogwindow, text = "Address : ") 
     self.lab_addr.pack() 

     # Refresh 
     self.btn_refresh = Tkinter.Button(self.dialogwindow, text = "Refresh", command = self.refresh) 
     self.btn_refresh.pack() 

     self.btn_cpe_exit = Tkinter.Button(self.dialogwindow, text = "Exit", command = self.dialogwindow.destroy) 
     self.btn_cpe_exit.pack() 

     # This is the important line: It tells the main-window to lock: 
     self.dialogwindow.grab_set() 

     self.Listing() 
     self.lab_addr['text'] = "address : " + self.addr; 

    def Listing(self): 
     print "hola" 
     # access the db and set the address into self.addr 

mw = Tkinter.Tk() 
app=myWindow() 
mw.option_add("*font", ("Arial", 15, "normal")) 
mw.geometry("+250+200") 
mw.title("Example of Custom Dialog-Window") 
mw.mainloop() 

&也,儘量不包括在構造函數中的UI部分。告訴我,如果有幫助

+0

沒有好友。請在Listing()函數中添加time.sleep(5)並對其進行測試。 –

+0

兄弟,我已經給你了它的要點..你想要運行的方式**清單()**是你的想法。 你可以使用睡眠,甚至可以使用一個按鈕來調用它。在這裏,對話框會先出現,休息是讓你決定 你甚至可以調用** Listing()**在窗口被銷燬後 – Gogo