Advertisement
Guest User

Tkinter example

a guest
Oct 12th, 2016
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. import Tkinter as tk
  2.  
  3. TITLE_FONT = ("Helvetica", 18, "bold")
  4. class SampleApp(tk.Tk):
  5.     def __init__(self, *args, **kwargs):
  6.  
  7.         tk.Tk.__init__(self, *args, **kwargs)
  8.         container = tk.Frame(self)
  9.         self.title(" Allocation")
  10.         width, height = self.winfo_screenwidth(), self.winfo_screenheight()
  11.         self.geometry('%dx%d+0+0' % (width,height))
  12.         self.state('zoomed')
  13.         container.pack(side="top", fill="both", expand=True)
  14.         container.grid_rowconfigure(0, weight=1)
  15.         container.grid_columnconfigure(0, weight=1)
  16.  
  17.         self.frames = {}
  18.         for F in (StartPage, PageOne, PageTwo):
  19.             frame = F(container, self)
  20.             self.frames[F] = frame
  21.             frame.grid(row=0, column=0, sticky="nsew")
  22.  
  23.         self.show_frame(StartPage)
  24.  
  25.     def show_frame(self, c):
  26.         frame = self.frames[c]
  27.         frame.tkraise()
  28.  
  29. class StartPage(tk.Frame):
  30.     def __init__(self, parent, controller):
  31.         tk.Frame.__init__(self, parent)
  32.         self.controller = controller
  33.         label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
  34.         label.place(x=0,y=0,width=592,height=44)
  35.         frame1 = tk.Frame(self)
  36.         tk.Label(frame1, bd=5,bg="black",text="  Enter text :  ",font=("Helvetica", 14),fg="light green",width=21).pack(side=tk.LEFT)
  37.         self.emp=tk.Entry(frame1, bd =5,font=("Times New Roman", 14),width=25)
  38.         self.emp.pack(side=tk.LEFT)
  39.         frame1.place(x=400,y=160)
  40.  
  41.         button1 = tk.Button(self, text="Go to Page One",
  42.                         command=lambda: self.go_to_page_one())
  43.         button2 = tk.Button(self, text="Go to Page two",
  44.                         command=lambda: controller.show_frame(PageTwo))
  45.         button3 = tk.Button(self, text="Exit",
  46.                         command=self.quit)
  47.         button1.place(x=100,y=406,width=200,height=44)
  48.         button2.place(x=300,y=406,width=200,height=44)
  49.         button3.place(x=500,y=406,width=80,height=44)
  50.  
  51.     def go_to_page_one(self):
  52.         self.controller.SomeVar = self.emp.get()
  53.         self.controller.frames[PageOne].correct_label()
  54.         self.controller.show_frame(PageOne)
  55.  
  56. class PageOne(tk.Frame):
  57.     def __init__(self, parent, controller):
  58.         self.controller = controller
  59.         tk.Frame.__init__(self, parent)
  60.         self.label = tk.Label(self, text="This is page one", font=TITLE_FONT)
  61.         self.label.place(x=0,y=0,width=592,height=44)
  62.  
  63.         button1 = tk.Button(self, text="Go to Start Page",
  64.                         command=lambda: controller.show_frame(StartPage))
  65.     #button2 = tk.Button(self, text="Go to Page two",
  66.      #                   command=lambda: controller.show_frame(PageTwo))
  67.         button3 = tk.Button(self, text="Exit",
  68.                         command=self.quit)
  69.         button1.place(x=100,y=406,width=200,height=44)
  70.         button3.place(x=300,y=406,width=200,height=44)
  71.  
  72.     def correct_label(self):
  73.         self.label.config(text=self.controller.SomeVar)
  74.  
  75. class PageTwo(tk.Frame):
  76.     def __init__(self, parent, controller):
  77.         tk.Frame.__init__(self, parent)
  78.         label = tk.Label(self, text="This is page two", font=TITLE_FONT)
  79.         label.place(x=0,y=0,width=592,height=44)
  80.  
  81.         button1 = tk.Button(self, text="Go to Start Page",
  82.                         command=lambda: controller.show_frame(StartPage))
  83.     #button2 = tk.Button(self, text="Go to Page two",
  84.      #                   command=lambda: controller.show_frame(PageTwo))
  85.         button3 = tk.Button(self, text="Exit",
  86.                         command=self.quit)
  87.         button1.place(x=100,y=406,width=200,height=44)
  88.         button3.place(x=300,y=406,width=200,height=44)
  89.  
  90. if __name__ == "__main__":
  91.     app = SampleApp()
  92.     app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement