我在Win机器上运行Tkinter 3.5,当我运行此代码时,我得到两个窗口.我只期待一个.顺便说一下,我从网上得到了代码.它工作正常,除了困扰我第二个(在backgorund)窗口.基本上是一个小部件,通过按钮导航到不同的窗口(页面).
#!/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()
Run Code Online (Sandbox Code Playgroud)
附加的空白窗口是根窗口.添加电话
root.withdraw()
Run Code Online (Sandbox Code Playgroud)
就root.title(' TK Wizards ')在线下,应该做的伎俩
在该main区域中,您创建一个tkinter对象,它将生成一个窗口:
root = tk.Tk()
Run Code Online (Sandbox Code Playgroud)
然后,在Wizard类中__init__创建一个Toplevel对象:
tk.Toplevel.__init__(self)
Run Code Online (Sandbox Code Playgroud)
所以您实际上是在这个新窗口中创建 GUI Toplevel。您可以更改程序以在根窗口中创建应用程序,这需要更改类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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5112 次 |
| 最近记录: |