2014-04-23 1 views
0

현재 작업중인 작업에 대한 GUI를 구현하려고합니다. 나는 다음/이전 버튼을 사용하여 페이지간에 갈 수있는 3 개의 페이지가있는 GUI를 만드는 오픈 소스 코드 (아래 참조)를 발견했다. 코드를 실행하면 각 버튼 등이 내재되어있는 것을 볼 수 있습니다.여러 단추가있는 파이썬에서 다중 페이지 GUI 사용

그러나 코드를 실행하고 "카운트 ++"버튼을 클릭하면 전체 카운트가 이되고 개별 페이지의 카운트가 증가하지 않습니다 (예 : 1 페이지에 있고 ++ 카운트를 4 번 클릭하면 여전히 페이지 2 또는 3, 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() 
+0

입니다 표준 대화 상자 .htm). 먼저, 'tkMessageBox 가져 오기'를 실행 한 다음,'tkMessageBox.showinfo (title, message)'를 사용하여 엽니 다. 이렇게하면 코드를 실행하려는 Windows가 아닌 사용자도 쉽게 사용할 수 있습니다. – FabienAndre

+0

주목! 댓글을 주셔서 감사합니다 @FabienAndre – gussilago

답변

0

귀하의 update_click 방법

덕분에, 당신의 마법사의 속성 click_count에서 작동합니다. 다른 수를 원한다면 페이지에 대한 클래스를 만들 수 있습니다. 따라서 각 개체가 자체 개수를 관리하거나 _children 목록을 처리하는 것과 같은 방식으로 목록에서 여러 카운터를 관리 할 수 ​​있습니다.

앞의 경우에는 _wizard_buttons 본문을 생성자로 사용하여 ttk.Frame을 상속하는 페이지 클래스를 만들 수 있습니다. 후자의 경우에, 당신은 당신 can not handle it though a variable을 텍스트 위젯 업데이트와 관련하여이

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를 계속하는 경우, 당신이 원하는 것 무엇 일반적인 조리법 http://effbot.org/tkinterbook/tkinter- (당신이 [tkMessageBox] 사용할 수있는 대화 상자를 열 수있는 휴대용 방법을

text.delete(1.0, END) 
text.insert(END, content) 
+0

합리적인 것 같지만 각 페이지 (또는 하위)에 실제로 도착하는 방법을 알아내는 데 어려움이 있습니다. 따라서 각 페이지에 대한 수업을 만듭니다 ... @FabienAndre – gussilago

+0

감사합니다. @FabienAndre! 실제로 스마트 솔루션, _children-list 방법론을 모방합니다. – gussilago