57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
import tkinter
|
|
from tkinter import ttk
|
|
|
|
class Adder(ttk.Frame):
|
|
def __init__(self, parent, *args, **kwargs):
|
|
ttk.Frame.__init__(self, parent, *args, **kwargs)
|
|
self.root = parent
|
|
self.init_gui()
|
|
|
|
def on_quit(self):
|
|
self.root.destroy()
|
|
|
|
|
|
def calculate(self):
|
|
num1 = int(self.num1_entry.get())
|
|
num2 = int(self.num2_entry.get())
|
|
num3 = num1 + num2
|
|
self.answer_label['text'] = num3
|
|
|
|
def init_gui(self):
|
|
self.root.title("Number Adder")
|
|
|
|
self.grid(column=0, row=0, sticky='nsew')
|
|
|
|
|
|
self.num1_entry = ttk.Entry(self, width=5)
|
|
self.num1_entry.grid(column=1, row=2)
|
|
|
|
self.num2_entry = ttk.Entry(self, width=5)
|
|
self.num2_entry.grid(column=3, row=2)
|
|
|
|
self.calc_button = ttk.Button(self, text='Calculate', command=self.calculate)
|
|
self.calc_button.grid(column=0, row=3, columnspan=2)
|
|
|
|
self.quit_button = ttk.Button(self, text='Quit', command=self.on_quit)
|
|
self.quit_button.grid(column=2, row=3, columnspan=2)
|
|
|
|
self.answer_frame = ttk.LabelFrame(self, text='Answer', height=100)
|
|
self.answer_frame.grid(column=0, row=4, columnspan=4, sticky='nesw')
|
|
|
|
self.answer_label = ttk.Label(self.answer_frame, text='')
|
|
self.answer_label.grid(column=0, row=0)
|
|
|
|
ttk.Label(self, text='Number Adder').grid(column=0, row=0, columnspan=4)
|
|
ttk.Label(self, text='Number One:').grid(column=0, row=2, sticky='w')
|
|
ttk.Label(self, text='Number Two:').grid(column=2, row=2, sticky='w')
|
|
|
|
ttk.Separator(self, orient='horizontal').grid(column=0, row=1, columnspan=4, sticky='ew')
|
|
for child in self.winfo_children():
|
|
child.grid_configure(padx=5, pady=5)
|
|
|
|
#if this is the main script
|
|
if __name__ == '__main__':
|
|
root = tkinter.Tk()
|
|
Adder(root)
|
|
root.mainloop()
|