Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.4 kB
1
Indexable
Never
import tkinter as tk
import tkinter.font as tkf
class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self,master)
        self.pack(expand=1, fill=tk.BOTH)
        self.createWidgets()

    def createWidgets(self):
        self.fntKey = tkf.Font(family="Seoge UI Symbol", size=12, weight="normal")
        self.fntResult = tkf.Font(family="Seoge UI Symbol", size=12, weight="bold")
        self.fntKeyBold = tkf.Font(family="Seoge UI Symbol", size=14, weight="bold")
        self.result = tk.StringVar(self, "0.0")
        self.config(bg="gray90")
        self.lblResult = tk.Label(self, text="0.0", font=self.fntResult, bg="gray90", textvariable=self.result)
        self.lblResult.grid(row=0, column=0, columnspan=4, sticky=tk.N+tk.S+tk.E, ipadx=8, ipady=8)
        labels = ["%", '√', 'x²', '1/x', 'CE','C','\u232b','\u00f7',"7","8","9","\u00d7", "4", "5", "6", "-", "1", "2", "3", "+","±", "0",".","="]
        for i, label in enumerate(labels):
            r = i // 4 + 1
            c = i%4
            btnKey = tk.Button(self, text=label, relief=tk.FLAT, command=lambda value = label: self.calc(value))
            btnKey["bg"] = "white" if label.isdigit() else "gray90"
            btnKey["font"] = self.fntKeyBold if label.isdigit() else self.fntKey
            btnKey.bind("<Enter>", lambda event: event.widget.configure(bg="gray70"))
            btnKey.bind("<Leave>", lambda event: event.widget.configure(bg="white" if event.widget["text"].isdigit() else "gray80"), add="+")
            btnKey.grid(row=r, column=c, sticky=tk.N+tk.S+tk.E+tk.W, padx=1,pady=1)

            self.rowconfigure(r, weight=1 )
            self.columnconfigure(c, weight = 1)
        lblHistory = tk.Label(self, text="There is no history yet", bg="gray90")
        lblHistory.grid(row=0, column=4, rowspan=7, sticky=tk.N+tk.W, padx=8, pady=8)
    def calc(self, value):
        if value.isdigit():
            value=int(value)
            if float (self.result.get()) == 0:
                self.result.set("")
            self.result.set(self.result.get() + str(value))
        else:
            if value == "CE":
                self.result.set("0.0")






# Hlavné okno
root = tk.Tk()
app = Application(master=root)
root.title("Kalkulačka")


# Hlavná slučka
root.mainloop()
Leave a Comment