Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # screenshot: http://imgur.com/5upiVMQ
- ### main.py ###
- from main_window import MainWindow
- mainWindow = MainWindow()
- mainWindow.mainloop()
- ### main_window.py ###
- import tkinter
- from calc_widget import CalcWidget
- from next_window import NextWindow
- class MainWindow(tkinter.Tk):
- def __init__(self):
- super().__init__()
- self.create_gui()
- def create_gui(self):
- self.calc_1 = CalcWidget(self, 'Add', command=lambda x, y: x+y)
- self.calc_1.grid(row=0, column=0)
- self.calc_2 = CalcWidget(self, 'Substract', command=lambda x, y: x-y)
- self.calc_2.grid(row=1, column=0)
- self.calc_3 = CalcWidget(self, 'Multiply', command=lambda x, y: x*y)
- self.calc_3.grid(row=2, column=0)
- self.calc_4 = CalcWidget(self, 'Divide', command=lambda x, y: x/y)
- self.calc_4.grid(row=3, column=0)
- self.button_next_window = tkinter.Button(self, text='Show next window', command=self.show_next_window)
- self.button_next_window.grid(row=4, column=0)
- self.button_close = tkinter.Button(self, text='Quit', command=self.destroy)
- self.button_close.grid(row=5, column=0)
- def show_next_window(self):
- next_window = NextWindow(self)
- ### calc_widget.py ###
- import tkinter
- class CalcWidget(tkinter.Frame):
- def __init__(self, parent, text, command):
- super().__init__(parent)
- self.button_text = text
- self.command = command
- self.create_gui()
- def create_gui(self):
- self.entry1 = tkinter.Entry(self)
- self.entry1.grid(row=0, column=0)
- self.entry2 = tkinter.Entry(self)
- self.entry2.grid(row=1, column=0)
- self.button_calc = tkinter.Button(self, text=self.button_text, command=self.calc)
- self.button_calc.grid(row=2, column=0)
- self.label_result = tkinter.Label(self, text='No result')
- self.label_result.grid(row=3, column=0)
- def calc(self):
- arg1 = int(self.entry1.get())
- arg2 = int(self.entry2.get())
- result = self.command(arg1, arg2)
- text_result = 'Result is: {}'.format(result)
- print(text_result)
- self.label_result.configure(text=text_result)
- ### next_window.py ###
- import tkinter
- class NextWindow(tkinter.Toplevel):
- def __init__(self, parent):
- super().__init__(parent)
- self.create_gui()
- def create_gui(self):
- self.label_result = tkinter.Label(self, text=' Hello World! ')
- self.label_result.grid(row=0, column=0)
- self.button_calc = tkinter.Button(self, text='Quit', command=self.destroy)
- self.button_calc.grid(row=1, column=0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement