user interface - How can I program a calculator with a GUI using tkinter in Python? -
i'm taking computing gcse , 1 of tasks our controlled assessment create calculator gui.
i'm able program simple calculator without gui don't understand how can done gui.
below code got teampython.wordpress.com, vaguely understand helpful if explain each individual step me.
# calc.py - python calculator tkinter import * class calc(): def __init__(self): self.total = 0 self.current = "" self.new_num = true self.op_pending = false self.op = "" self.eq = false def num_press(self, num): self.eq = false temp = text_box.get() temp2 = str(num) if self.new_num: self.current = temp2 self.new_num = false else: if temp2 == '.': if temp2 in temp: return self.current = temp + temp2 self.display(self.current) def calc_total(self): self.eq = true self.current = float(self.current) if self.op_pending == true: self.do_sum() else: self.total = float(text_box.get()) def display(self, value): text_box.delete(0, end) text_box.insert(0, value) def do_sum(self): if self.op == "add": self.total += self.current if self.op == "minus": self.total -= self.current if self.op == "times": self.total *= self.current if self.op == "divide": self.total /= self.current self.new_num = true self.op_pending = false self.display(self.total) def operation(self, op): self.current = float(self.current) if self.op_pending: self.do_sum() elif not self.eq: self.total = self.current self.new_num = true self.op_pending = true self.op = op self.eq = false def cancel(self): self.eq = false self.current = "0" self.display(0) self.new_num = true def all_cancel(self): self.cancel() self.total = 0 def sign(self): self.eq = false self.current = -(float(text_box.get())) self.display(self.current) sum1 = calc() root = tk() calc = frame(root) calc.grid() root.title("calculator") text_box = entry(calc, justify=right) text_box.grid(row = 0, column = 0, columnspan = 3, pady = 5) text_box.insert(0, "0") numbers = "789456123" = 0 bttn = [] j in range(1,4): k in range(3): bttn.append(button(calc, text = numbers[i])) bttn[i].grid(row = j, column = k, pady = 5) bttn[i]["command"] = lambda x = numbers[i]: sum1.num_press(x) += 1 bttn_0 = button(calc, text = "0") bttn_0["command"] = lambda: sum1.num_press(0) bttn_0.grid(row = 4, column = 1, pady = 5) bttn_div = button(calc, text = chr(247)) bttn_div["command"] = lambda: sum1.operation("divide") bttn_div.grid(row = 1, column = 3, pady = 5) bttn_mult = button(calc, text = "x") bttn_mult["command"] = lambda: sum1.operation("times") bttn_mult.grid(row = 2, column = 3, pady = 5) minus = button(calc, text = "-") minus["command"] = lambda: sum1.operation("minus") minus.grid(row = 3, column = 3, pady = 5) point = button(calc, text = ".") point["command"] = lambda: sum1.num_press(".") point.grid(row = 4, column = 0, pady = 5) add = button(calc, text = "+") add["command"] = lambda: sum1.operation("add") add.grid(row = 4, column = 3, pady = 5) neg= button(calc, text = "+/-") neg["command"] = sum1.sign neg.grid(row = 5, column = 0, pady = 5) clear = button(calc, text = "c") clear["command"] = sum1.cancel clear.grid(row = 5, column = 1, pady = 5) all_clear = button(calc, text = "ac") all_clear["command"] = sum1.all_cancel all_clear.grid(row = 5, column = 2, pady = 5) equals = button(calc, text = "=") equals["command"] = sum1.calc_total equals.grid(row = 5, column = 3, pady = 5) root.mainloop()
so, i'll explain code you've given best understand it. class calc()
contains functions piece of code. structure means main gui (set later) can access each function easily. within calc()
class, have functions (denoted def
etc.). these contain various methods code computes it's output.
outside of class, have tkinter ui code. code builds window inside of various buttons , displays sit. positioning of buttons , text fields in case governed 'grid' method. can see, every time code sets object (here frame
, button
, entry
objects), there associated .grid(row=x, column=y...etc)
. specifies relative positions of each object in ui. example, using grid method stack 2 objects giving first object row=1, column=0 , second row=2, column=0 etc.
the loop:
for j in range(1,4): k in range(3): bttn.append(button(calc, text = numbers[i])) bttn[i].grid(row = j, column = k, pady = 5) bttn[i]["command"] = lambda x = numbers[i]: sum1.num_press(x) += 1
is part of ui not straightforward see if you're starting out. in essence doing building buttons automatically (saving time code each 1 individually). first 2 lines in loop (hopefully) looping on values in specified ranges. beneath line bttn.append(...
creates button object in calc frame set earlier calc = frame(root), button text being given list of numbers (literally numbers="789456123"
above). initially, = 0, numbers[i] return first element in list numbers, numbers[i] i=1 (the next time loops over) give 8 , on. bttn.grid(row = j, column = k, pady = 5)
uses grid positioning method mentioned earlier, here j , k given values in loop (values between 1 , 4 in case of rows, , values between 0 , 2 - 3 columns - in case of columns). if run code, can see position keypad buttons inputting numbers. last line in loop (besides i+=1, i.e add 1 i), handles assigning button command. command call associated function, in case numpress
function in calc()
. may able see, numpress
updates display whatever number press.
the final block of code in example handles remaining operations calculator, , notice each follows above pattern of creating button, assigning command , positioning button in ui. i've explained above, may able see each of remaining functions in calc()
governs arithmetical operation, or clear etc.
i realise wall of text, hope helps! if i've been unclear or there in particular don't understand, let me know, i'll try , explain (i've not long learnt myself!).
you might find tkinter ui guide useful, learnt lot of basics guide.
good luck
Comments
Post a Comment