在Python中创建多个页面的GUI,包含多个按钮
我现在正在为工作上的一个任务实现一个图形用户界面(GUI)。我找到了一些开源代码(见下文),这个代码可以创建一个有三个页面的GUI,用户可以通过“下一页”和“上一页”按钮在页面之间切换。如果你运行这个代码,就能看到每个按钮的功能是什么。
但是,当你运行代码并点击“count++”按钮时,整体的计数会增加一而不是每个页面的计数(比如在第一页上点击“count++”四次,第二页或第三页的计数也会变成4,而不是0)。当我尝试更新每个页面上文本框里的数字(应该是点击次数)时,也无法更新。我不太确定如何才能访问每个单独页面的文本框。
有没有什么建议可以帮助我解决这个问题?从长远来看,我希望能有下拉菜单,用户的选择会显示在每个单独的文本框里。
谢谢,
import ttk
from Tkinter import *
import tkMessageBox
class Wizard(object, ttk.Notebook):
def __init__(self, master=None, **kw):
npages = kw.pop('npages', 3)
kw['style'] = 'Wizard.TNotebook'
ttk.Style(master).layout('Wizard.TNotebook.Tab', '')
ttk.Notebook.__init__(self, master, **kw)
self._children = {}
self.click_count = 0
self.txt_var = "Default"
for page in range(npages):
self.add_empty_page()
self.current = 0
self._wizard_buttons()
def _wizard_buttons(self):
"""Place wizard buttons in the pages."""
for indx, child in self._children.iteritems():
btnframe = ttk.Frame(child)
btnframe.pack(side='left', fill='x', padx=6, pady=4)
txtframe = ttk.Frame(child)
txtframe.pack(side='right', fill='x', padx=6, pady=4)
nextbtn = ttk.Button(btnframe, text="Next", command=self.next_page)
nextbtn.pack(side='top', padx=6)
countbtn = ttk.Button(txtframe, text="Count++..", command=self.update_click)
countbtn.grid(column=0,row=0)
txtBox = Text(txtframe,width = 50, height = 20, wrap = WORD)
txtBox.grid(column=1,row=0)
txtBox.insert(0.0, self.txt_var)
rstbtn = ttk.Button(btnframe, text="Reset count!", command=self.reset_count)
rstbtn.pack(side='top', padx=6)
if indx != 0:
prevbtn = ttk.Button(btnframe, text="Previous",
command=self.prev_page)
prevbtn.pack(side='right', anchor='e', padx=6)
if indx == len(self._children) - 1:
nextbtn.configure(text="Finish", command=self.close)
def next_page(self):
self.current += 1
def prev_page(self):
self.current -= 1
def close(self):
self.master.destroy()
def add_empty_page(self):
child = ttk.Frame(self)
self._children[len(self._children)] = child
self.add(child)
def add_page_body(self, body):
body.pack(side='top', fill='both', padx=6, pady=12)
def page_container(self, page_num):
if page_num in self._children:
return self._children[page_num]
else:
raise KeyError("Invalid page: %s" % page_num)
def _get_current(self):
return self._current
def _set_current(self, curr):
if curr not in self._children:
raise KeyError("Invalid page: %s" % curr)
self._current = curr
self.select(self._children[self._current])
current = property(_get_current, _set_current)
def update_click(self):
self.click_count += 1
message = "You have clicked %s times now!" % str(self.click_count)
tkMessageBox.showinfo("monkeybar", message)
self.txt_var = "Number of clicks: %s" % str(self.click_count) #this will not change the text in the textbox!
def reset_count(self):
message = "Count is now 0."
#ctypes.windll.user32.MessageBoxA(0, message, "monkeybar", 1)
tkMessageBox.showinfo("monkeybar", message)
self.click_count = 0
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
def demo():
root = Tk()
nbrpages = 7
wizard = Wizard(npages=nbrpages)
wizard.master.minsize(400, 350)
wizard.master.title("test of GUI")
pages = range(nbrpages)
for p in pages:
pages[p] = ttk.Label(wizard.page_container(p), text='Page %s'%str(p+1))
wizard.add_page_body(pages[p])
wizard.pack(fill='both', expand=True)
root.mainloop()
if __name__ == "__main__":
demo()
1 个回答
0
你的 update_click
方法是用来操作你这个向导的 click_count
属性的。如果你想要不同的计数方式,你可以选择为每个页面创建一个类,这样每个对象就可以管理自己的计数,或者你也可以像处理 _children
列表那样,管理多个计数器,比如用一个列表来存放它们。
如果选择第一种方法,你可以创建一个页面类,继承自 ttk.Frame,并把你的 _wizard_buttons
循环的内容放在构造函数里。对于第二种情况,你可以尝试类似下面的代码:
class Wizard(object, ttk.Notebook):
def __init__(self, master=None, **kw):
[...]
#replace self.click_count = 0 with
self.click_counters = [0 for i in range(npages)]
def update_click(self):
self.click_counters[self.current] += 1
# and so on...
关于文本小部件的更新,你 不能通过变量来处理,它适用于 Entry
(单行文本框),但不适用于 Text
(多行富文本框)。如果你继续使用 Text
,那么通常的做法是你想要的效果是:
text.delete(1.0, END)
text.insert(END, content)