2016-10-11 5 views
0

私のプログラムは、TkinterのインポートからTK として*python tkinterのあるページから別のページにデータを渡すには?

TITLE_FONT = ("Helvetica", 18, "bold") 
class SampleApp(tk.Tk): 
    def __init__(self, *args, **kwargs): 

     tk.Tk.__init__(self, *args, **kwargs) 
     container = tk.Frame(self) 
     self.title(" Allocation") 
     width, height = self.winfo_screenwidth(), self.winfo_screenheight() 
     self.geometry('%dx%d+0+0' % (width,height)) 
     self.state('zoomed') 
     self.wm_iconbitmap('icon.ico') 
     container.pack(side="top", fill="both", expand=True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     self.frames = {} 
     for F in (StartPage, PageOne, PageTwo): 
      frame = F(container, self) 
      self.frames[F] = frame 
      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame(StartPage) 

    def show_frame(self, c): 
     frame = self.frames[c] 
     frame.tkraise() 

class StartPage(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     logo = tk.PhotoImage(file="backesh.ppm") 
     BGlabel = tk.Label(self,image=logo) 
     BGlabel.image = logo 
     BGlabel.place(x=0,y=0,width=592,height=450) 
     label = tk.Label(self, text="This is the start page", font=TITLE_FONT) 
     label.place(x=0,y=0,width=592,height=44) 
     frame1 = Frame(self) 
     Label(frame1, bd=5,bg="black",text=" Enter text : ",font=("Helvetica", 14),fg="light green",width=21).pack(side=LEFT) 
     emp=Entry(frame1, bd =5,font=("Times New Roman", 14),width=25) 
     emp.pack(side=LEFT) 
     frame1.place(x=400,y=160) 

     button1 = tk.Button(self, text="Go to Page One", 
         command=lambda: controller.show_frame(PageOne)) 
     button2 = tk.Button(self, text="Go to Page two", 
         command=lambda: controller.show_frame(PageTwo)) 
     button3 = tk.Button(self, text="Exit", 
         command=self.quit) 
     button1.place(x=100,y=406,width=200,height=44) 
     button2.place(x=300,y=406,width=200,height=44) 
     button3.place(x=500,y=406,width=80,height=44) 


class PageOne(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     logo = tk.PhotoImage(file="backesh.ppm") 
     BGlabel = tk.Label(self,image=logo) 
     BGlabel.image = logo 
     BGlabel.place(x=0,y=0,width=592,height=450) 
     label = tk.Label(self, text="This is page one", font=TITLE_FONT) 
     label.place(x=0,y=0,width=592,height=44) 

     button1 = tk.Button(self, text="Go to Start Page", 
         command=lambda: controller.show_frame(StartPage)) 
    #button2 = tk.Button(self, text="Go to Page two", 
    #     command=lambda: controller.show_frame(PageTwo)) 
     button3 = tk.Button(self, text="Exit", 
         command=self.quit) 
     button1.place(x=100,y=406,width=200,height=44) 
     button3.place(x=300,y=406,width=200,height=44) 

class PageTwo(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     logo = tk.PhotoImage(file="backesh.ppm") 
     BGlabel = tk.Label(self,image=logo) 
     BGlabel.image = logo 
     BGlabel.place(x=0,y=0,width=592,height=450) 
     label = tk.Label(self, text="This is page two", font=TITLE_FONT) 
     label.place(x=0,y=0,width=592,height=44) 

     button1 = tk.Button(self, text="Go to Start Page", 
         command=lambda: controller.show_frame(StartPage)) 
    #button2 = tk.Button(self, text="Go to Page two", 
    #     command=lambda: controller.show_frame(PageTwo)) 
     button3 = tk.Button(self, text="Exit", 
         command=self.quit) 
     button1.place(x=100,y=406,width=200,height=44) 
     button3.place(x=300,y=406,width=200,height=44) 

if __name__ == "__main__": 
    app = SampleApp() 
    app.mainloop() 

この.. 輸入のTkinterである私は、スタートページからエントリーのテキストデータを取得し、PageOneにラベルとして、それを表示したいです。どうやってするの?私はこれに新しいです。コードを入力してください。 ありがとうございます。まず

答えて

0

、クラスPageOneスタートページのコードを修正し、__init__関数に=コントローラself.controllerを追加します。

class PageOne(tk.Frame): 
    def __init__(self, parent, controller): 
     #your code 
     self.controler = controller 

は、スタートページで入力フィールドの前に、ラベルの前に自己を追加します。

#entry in StartPage 
self.emp=tk.Entry(frame1, bd =5,font=("Times New Roman", 14),width=25) 
self.emp.pack(side=tk.LEFT) 

#label in PageOne 
self.label = tk.Label(self, text="This is page one", font=TITLE_FONT) 
self.label.place(x=0,y=0,width=592,height=44) 

次にスタートページクラスに機能go_to_page_oneを追加します:PageOneで

スタートページクラスの変更コマンドでボタン1で
def go_to_page_one(self): 
    self.controller.SomeVar = self.emp.get() #save text from entry to some var 
    self.controller.frames[PageOne].correct_label() #call correct_label function 
    self.controller.show_frame(PageOne) #show page one 

ラムダには:self.go_to_page_one():

button1 = tk.Button(self, text="Go to Page One", 
        command=lambda: self.go_to_page_one()) 

で最後のクラスPageOneに機能正しいラベルを追加します。

def correct_label(self): 
    self.label.config(text=self.controller.SomeVar) #correct the label 
+0

先生そのフルコードを書き直して助けてください –

+0

私の投稿を編集しました。やってみよう。あなたのコードには完全な例(http://pastebin.com/YbbFZ3uV)もあります。私はそれらの画像とアイコンを持っていないので、私は別の方法でエラーが発生したので、コードからいくつかのアイコンと画像を削除しました。 – Silko

関連する問題