how can i produce my buttons using while loop and a list if stirng

# python 2.x
#from Tkinter import *
#from tkFont import Font

# python 3.x
from tkinter import * 
from tkinter.font import Font

def button(frame, text, command=None):
    ft = Font(family=('Verdana'), size=14)
    return Button(frame, text=text, font=ft, width=3, command=command)

def frame(frame, side=LEFT, bg="black"):
    f = Frame(frame, background=bg, padx=5, pady=5)
    f.pack(side=side, expand=YES, fill=BOTH)
    return f


class App:
    def __init__(self, tk):
        ft = Font(family=('Verdana'), size=14)
        main = frame(tk)
        l_frame = frame(main)
        r_frame = frame(main)
        calc_frame = frame(l_frame)

        self.btnquit = button(calc_frame, "Quit", tk.destroy)
        self.btnquit.pack(side = LEFT)

        self.input = Entry(calc_frame, font=ft, width=15, background="white")
        self.input.pack(side=TOP)
        self.btn_frame = frame(calc_frame)
        x, y = 0, 0
        for key in ("()%C", "+-*/", "1234", "5678", "90.="):
            for c in key:
                if c == "=":
                    btn = button(self.btn_frame, c, self.equalAction)
                elif c == "C":
                    btn = button(self.btn_frame, c, self.cleanAction)
                else:
                    btn = button(self.btn_frame, c, lambda i=c: self.input.insert(INSERT, i))
                    main.bind_all(c, lambda event, i=c:self.input.insert(INSERT, i)) # numbers binding
                btn.grid(row=x, column=y)
                y += 1
            x += 1
            y = 0
        self.log = Text(r_frame, font=Font(family=('Verdana'), size=10), width=25, height=14, background="yellow")
        self.log.pack(side=RIGHT)
        main.bind_all('<BackSpace>', self.cleanInput)
        #~ main.bind_all('1', lambda event, i='1':self.insertNumber(i))
        #~ main.bind_all('2', lambda event, i='2':self.insertNumber(i))


        main.bind_all('<Escape>', self.deleteLastDigit)

        self.btn_backspace = button(calc_frame, "Backspace", self.deleteLastDigit)
        self.btn_backspace.pack(side = LEFT)

    # event=None to use function in command= and in binding
    def deleteLastDigit(self, event=None): 
        text = self.input.get()[:-1]
        self.input.delete(0, END)
        self.input.insert(INSERT, text)




    def insertNumber(self, number):
        self.input.insert(INSERT, number)

    def cleanInput(self, event):
        self.input.delete(0, END)
        self.log.delete(1.0, END)

    def cleanAction(self):
        self.input.delete(0, END)

    def equalAction(self):
        tmp = self.input.get()
        try:
            result = tmp + "=" + str(eval(tmp))
            self.log.insert(1.0, result + "\n");
            print(result)
        except Exception:
            self.log.insert(1.0, "Wrong expression\n");


if __name__ == '__main__':
    root = Tk()
    root.title("Calculator")
    root.geometry()
    app = App(root)
    root.mainloop()

Edited 3 Years Ago by dean.ong.14

Am I going blind?

I can't see a while loop in your code.

Also, you can add a few "test prints" to help you debug this.

Edited 3 Years Ago by vegaseat

This article has been dead for over six months. Start a new discussion instead.