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.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)
                    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")
        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()
            result = tmp + "=" + str(eval(tmp))
            self.log.insert(1.0, result + "\n");
        except Exception:
            self.log.insert(1.0, "Wrong expression\n");

if __name__ == '__main__':
    root = Tk()
    app = App(root)

Edited by dean.ong.14

3 Years
Discussion Span
Last Post by vegaseat
Featured Replies
  • Could you please explain your goal in this a little? We need more context to be able to give a meaningful answer. Read More


Could you please explain your goal in this a little? We need more context to be able to give a meaningful answer.


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 by vegaseat

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.