Advertisement
furas

tkinter in many files using classes

Apr 16th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. # screenshot: http://imgur.com/5upiVMQ
  2.  
  3. ### main.py ###
  4.  
  5. from main_window import MainWindow
  6.  
  7.  
  8. mainWindow = MainWindow()
  9. mainWindow.mainloop()
  10.  
  11.  
  12.  
  13. ### main_window.py ###
  14.  
  15. import tkinter
  16. from calc_widget import CalcWidget
  17. from next_window import NextWindow
  18.  
  19.  
  20. class MainWindow(tkinter.Tk):
  21.    
  22.     def __init__(self):
  23.         super().__init__()
  24.         self.create_gui()
  25.        
  26.     def create_gui(self):
  27.         self.calc_1 = CalcWidget(self, 'Add', command=lambda x, y: x+y)
  28.         self.calc_1.grid(row=0, column=0)
  29.  
  30.         self.calc_2 = CalcWidget(self, 'Substract', command=lambda x, y: x-y)
  31.         self.calc_2.grid(row=1, column=0)
  32.  
  33.         self.calc_3 = CalcWidget(self, 'Multiply', command=lambda x, y: x*y)
  34.         self.calc_3.grid(row=2, column=0)
  35.        
  36.         self.calc_4 = CalcWidget(self, 'Divide', command=lambda x, y: x/y)
  37.         self.calc_4.grid(row=3, column=0)
  38.  
  39.        
  40.         self.button_next_window = tkinter.Button(self, text='Show next window', command=self.show_next_window)
  41.         self.button_next_window.grid(row=4, column=0)
  42.  
  43.         self.button_close = tkinter.Button(self, text='Quit', command=self.destroy)
  44.         self.button_close.grid(row=5, column=0)
  45.  
  46.     def show_next_window(self):
  47.         next_window = NextWindow(self)
  48.        
  49.  
  50.  
  51. ### calc_widget.py ###
  52.  
  53. import tkinter
  54.  
  55.  
  56. class CalcWidget(tkinter.Frame):
  57.    
  58.     def __init__(self, parent, text, command):
  59.         super().__init__(parent)
  60.        
  61.         self.button_text = text
  62.         self.command = command
  63.        
  64.         self.create_gui()
  65.        
  66.     def create_gui(self):
  67.         self.entry1 = tkinter.Entry(self)
  68.         self.entry1.grid(row=0, column=0)
  69.  
  70.         self.entry2 = tkinter.Entry(self)
  71.         self.entry2.grid(row=1, column=0)
  72.  
  73.         self.button_calc = tkinter.Button(self, text=self.button_text, command=self.calc)
  74.         self.button_calc.grid(row=2, column=0)
  75.  
  76.         self.label_result = tkinter.Label(self, text='No result')
  77.         self.label_result.grid(row=3, column=0)
  78.  
  79.     def calc(self):
  80.  
  81.         arg1 = int(self.entry1.get())
  82.         arg2 = int(self.entry2.get())
  83.        
  84.         result =  self.command(arg1, arg2)
  85.        
  86.         text_result = 'Result is: {}'.format(result)
  87.        
  88.         print(text_result)
  89.         self.label_result.configure(text=text_result)
  90.  
  91.  
  92.  
  93. ### next_window.py ###
  94.  
  95. import tkinter
  96.  
  97.  
  98. class NextWindow(tkinter.Toplevel):
  99.    
  100.     def __init__(self, parent):
  101.         super().__init__(parent)
  102.         self.create_gui()
  103.        
  104.     def create_gui(self):
  105.         self.label_result = tkinter.Label(self, text='   Hello World!   ')
  106.         self.label_result.grid(row=0, column=0)
  107.  
  108.         self.button_calc = tkinter.Button(self, text='Quit', command=self.destroy)
  109.         self.button_calc.grid(row=1, column=0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement