2015-06-29 184 views
2

我在Win機器上運行Tkinter 3.5,當我運行此代碼時,我得到兩個窗口。我只期待一個。順便說一句,我得到了代碼形式的網絡。它工作正常,除了困擾我第二(在backgorund)窗口。 基本上是一個窗口小部件來瀏覽不同的窗口(頁面)與按鈕。Tkinter小部件打開兩個窗口

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# 

try: 
    import Tkinter as tk # Python2 
except ImportError: 
    import tkinter as tk # Python3 

class Wizard(tk.Toplevel): 
    def __init__(self, npages, master=None): 

     self.pages = [] 
     self.current = 0 
     tk.Toplevel.__init__(self) 

     self.attributes('-topmost', True) 


     for page in range(npages): 
      self.pages.append(tk.Frame(self)) 
     self.pages[0].pack(fill='both', expand=1) 
     self.__wizard_buttons() 

    def onQuit(self): 
     pass 

    def __wizard_buttons(self): 
     for indx, frm in enumerate(self.pages): 
      btnframe = tk.Frame(frm, bd=1, bg='#3C3B37') 
      btnframe.pack(side='bottom', fill='x') 
      nextbtn = tk.Button(btnframe, bd=0, bg='#F2F1F0', activebackground='#F58151', highlightcolor='red', cursor='hand2', text="Siguiente >>", width=10, command=self.__next_page) 
      nextbtn.pack(side='right', anchor='e', padx=5, pady=5) 
      if indx != 0: 
       prevbtn = tk.Button(btnframe, bd=0, bg='#F2F1F0', activebackground='#F58151', highlightcolor='red', cursor='hand2', text="<< Atras", width=10, command=self.__prev_page) 
       prevbtn.pack(side='right', anchor='e', padx=5, pady=5) 
       if indx == len(self.pages) - 1: 
        nextbtn.configure(text="Terminar", bd=0, bg='#F2F1F0', activebackground='#F58151', highlightcolor='red', cursor='hand2', command=self.close) 

    def __next_page(self): 
     if self.current == len(self.pages): 
      return 
     self.pages[self.current].pack_forget() 
     self.current += 1 
     self.pages[self.current].pack(fill='both', expand=1) 

    def __prev_page(self): 
     if self.current == 0: 
      return 
     self.pages[self.current].pack_forget() 
     self.current -= 1 
     self.pages[self.current].pack(fill='both', expand=1) 

    def add_page_body(self, body): 
     body.pack(side='top', fill='both', padx=6, pady=12) 

    def page(self, page_num): 
     try: 
      page = self.pages[page_num] 
     except KeyError("Pagina Invalida! : %s" % page_num): 
      return 0 
     return page 

    def close(self): 
     if self.validate(): 
      self.master.iconify() 
      print (' TK Wizard finished... ') 
      self.destroy() 
      self.master.destroy() 

    def validate(self): 
     return 1 

if __name__ == "__main__": 
    root = tk.Tk() 
    root.title(' TK Wizards ') 
    wizard = Wizard(npages=3, master=root) 
    wizard.minsize(400, 350) 
    page0 = tk.Label(wizard.page(0), text='Pagina 1: ...Bienvenido al Wizard de TK !') 
    page1 = tk.Label(wizard.page(1), text='Pagina 2: Acepta las condiciones de la WTFPL ?') 
    page2 = tk.Label(wizard.page(2), text='Pagina 3: Felicitaciones, nada no se ha instalado correctamente.') 
    wizard.add_page_body(page0) 
    wizard.add_page_body(page1) 
    wizard.add_page_body(page2) 
    root.mainloop() 

回答

4

附加的空白窗口是根窗口。呼叫添加到

root.withdraw() 

正下方的root.title(' TK Wizards ')線,應該做的伎倆

+0

這工作。謝謝 – Martin

+0

樂意提供幫助。請考慮[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)答案,如果它解決了您的問題,讓其他用戶知道問題已解決。 – maccartm

+0

簡單和工作的解決方案,謝謝。 –

0

main區域,爲您營造一個tkinter對象,這將產生一個窗口:

root = tk.Tk() 

然後,在Wizard類的__init__中,您創建了一個Toplevel對象:

tk.Toplevel.__init__(self) 

所以你真的在這個新的Toplevel窗口中創建GUI。您可以更改程序以在根窗口中創建應用程序,這需要將Wizard類更改爲從默認object繼承,並將程序更改爲根據保存的self.master根對象採取行動,該對象用於處理Wizard對象(它是不再是一個Toplevel對象)。

try: 
    import Tkinter as tk # Python2 
except ImportError: 
    import tkinter as tk # Python3 

class Wizard(object): 
    def __init__(self, npages, master=None): 

     self.pages = [] 
     self.current = 0 
     self.master = master 
     self.master.attributes('-topmost', True) 


     for page in range(npages): 
      self.pages.append(tk.Frame(self.master)) 
     self.pages[0].pack(fill='both', expand=1) 
     self.__wizard_buttons() 

    def onQuit(self): 
     pass 

    def __wizard_buttons(self): 
     for indx, frm in enumerate(self.pages): 
      btnframe = tk.Frame(frm, bd=1, bg='#3C3B37') 
      btnframe.pack(side='bottom', fill='x') 
      nextbtn = tk.Button(btnframe, bd=0, bg='#F2F1F0', activebackground='#F58151', highlightcolor='red', cursor='hand2', text="Siguiente >>", width=10, command=self.__next_page) 
      nextbtn.pack(side='right', anchor='e', padx=5, pady=5) 
      if indx != 0: 
       prevbtn = tk.Button(btnframe, bd=0, bg='#F2F1F0', activebackground='#F58151', highlightcolor='red', cursor='hand2', text="<< Atras", width=10, command=self.__prev_page) 
       prevbtn.pack(side='right', anchor='e', padx=5, pady=5) 
       if indx == len(self.pages) - 1: 
        nextbtn.configure(text="Terminar", bd=0, bg='#F2F1F0', activebackground='#F58151', highlightcolor='red', cursor='hand2', command=self.close) 

    def __next_page(self): 
     if self.current == len(self.pages): 
      return 
     self.pages[self.current].pack_forget() 
     self.current += 1 
     self.pages[self.current].pack(fill='both', expand=1) 

    def __prev_page(self): 
     if self.current == 0: 
      return 
     self.pages[self.current].pack_forget() 
     self.current -= 1 
     self.pages[self.current].pack(fill='both', expand=1) 

    def add_page_body(self, body): 
     body.pack(side='top', fill='both', padx=6, pady=12) 

    def page(self, page_num): 
     try: 
      page = self.pages[page_num] 
     except KeyError("Pagina Invalida! : %s" % page_num): 
      return 0 
     return page 

    def close(self): 
     if self.validate(): 
      self.master.iconify() 
      print (' TK Wizard finished... ') 
      self.destroy() 
      self.master.destroy() 

    def validate(self): 
     return 1 

if __name__ == "__main__": 
    root = tk.Tk() 
    root.title(' TK Wizards ') 
    wizard = Wizard(npages=3, master=root) 
    wizard.master.minsize(400, 350) 
    page0 = tk.Label(wizard.page(0), text='Pagina 1: ...Bienvenido al Wizard de TK !') 
    page1 = tk.Label(wizard.page(1), text='Pagina 2: Acepta las condiciones de la WTFPL ?') 
    page2 = tk.Label(wizard.page(2), text='Pagina 3: Felicitaciones, nada no se ha instalado correctamente.') 
    wizard.add_page_body(page0) 
    wizard.add_page_body(page1) 
    wizard.add_page_body(page2) 
    root.mainloop() 
+0

錯誤文件「C:\ Program Files \ Python 3.5 \ lib \ tkinter \ __ init__.py」,行2095,_setup self.tk = master.tk AttributeError:'嚮導'對象沒有屬性'tk' – Martin

+0

對不起,我無法重現那個錯誤。發佈的代碼在我的機器上正常工作(Windows 7,Python 3.4.2)。實際程序中的哪條線負責該錯誤? – TigerhawkT3

+0

TigerhawkT3,對不起。沒有做出所有建議的更改。您的代碼完美無缺。謝謝!!!! – Martin

相關問題