Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to make tkinter look better

# There a library that is based on tkinter which is custom tkinter
# You can see the documentation : https://github.com/TomSchimansky/CustomTkinter/wiki
# it looks really better and modern
# You can see the differnece between the tkinter copy of a program and the customtkinter copy:

# Tkinter copy: https://dl.dropbox.com/s/d95r1pie7dqddn4/python_d3hydWUufF.png?dl=0
# Custom Tkinter copy: https://dl.dropbox.com/s/w1u0mhw0chwfetq/python_MuyDj7n17B.png?dl=0

#tkinter code:
from tkinter import *

def eqFunc():
    try:
        result = eval((equation.get()).replace("×", "*").replace("÷", "/"))
        if result == int(result): result = int(result)
    except: result = "Error"

    equation.delete(0, "end")
    equation.insert(0, result)

buttons = ("1", "2", "3", "+", "4", "5", "6", "-", '7', '8', '9', '×', "(", ")", "=", "÷")

window = Tk()
window.title("Calculator")
window.geometry("525x580")
window.resizable(False, False)

equation = Entry(window, font=("Calibri", 48), width=16)
equation.pack(pady=2)

buttonsGrid = Frame()
buttonsGrid.pack()

fontsize = 40
column = -1
row = 0
for i in buttons:
    column += 1
    if column == 4:
        column = 0
        row += 1

    
    if i == "=":
        ButtonEq = Button(buttonsGrid,  text="=", font=("Calibri", fontsize), width=4, height=1, command=eqFunc)
        ButtonEq.grid(column=2, row=3, pady=5, padx=5)

    else: exec("Button"+i.replace("=", "eq").replace("(", "b1").replace(")", "b2").replace("+", "P").replace("-", "Min").replace("×", "Mul").replace("÷", "D")+" = Button(buttonsGrid,  text='"+i+"', font=('Calibri', fontsize), width=4, height=1, command=lambda: equation.insert(equation.index('insert'), '"+i+"')) 
Button"+i.replace("=", "eq").replace("(", "b1").replace(")", "b2").replace("+", "P").replace("-", "Min").replace("×", "Mul").replace("÷", "D")+".grid(row="+str(row)+", column="+str(column)+", padx=5, pady=5)")

window.mainloop()

#customtkinter code:
from customtkinter import *
set_appearance_mode("light")
set_default_color_theme("blue")

def eqFunc():
    try:
        result = eval((equation.get()).replace("×", "*").replace("÷", "/"))
        if result == int(result): result = int(result)
    except: result = "Error"

    equation.delete(0, "end")
    equation.insert(0, result)

buttons = ("1", "2", "3", "+", "4", "5", "6", "-", '7', '8', '9', '×', "(", ")", "=", "÷")

window = CTk()
window.title("Calculator")
window.geometry("500x580")
window.resizable(False, False)

equation = CTkEntry(window, text_font=("Calibri", 48), width=495)
equation.pack(pady=2)

buttonsGrid = CTkFrame()
buttonsGrid.pack()

column = -1
row = 0
for i in buttons:
    column += 1
    if column == 4:
        column = 0
        row += 1

    something = 1.75
    if i == "=":
        ButtonEq = CTkButton(buttonsGrid,  text="=", text_font=("Calibri", int(32*something)), fg_color='#fff', text_color='#000', hover_color='#e3e3e3', width=64*something, height=64*something, command=eqFunc)
        ButtonEq.grid(column=2, row=3, pady=5, padx=5)

    else: exec("Button"+i.replace("=", "eq").replace("(", "b1").replace(")", "b2").replace("+", "P").replace("-", "Min").replace("×", "Mul").replace("÷", "D")+" = CTkButton(buttonsGrid,  text='"+i+"', text_font=('Calibri', int(32*something)), fg_color='#fff', text_color='#000', hover_color='#e3e3e3', width=64*something, height=64*something, command=lambda: equation.insert(equation.cursorIndex(), '"+i+"')) 
Button"+i.replace("=", "eq").replace("(", "b1").replace(")", "b2").replace("+", "P").replace("-", "Min").replace("×", "Mul").replace("÷", "D")+".grid(row="+str(row)+", column="+str(column)+", padx=5, pady=5)")

window.mainloop()
 
PREVIOUS NEXT
Tagged: #tkinter
ADD COMMENT
Topic
Name
6+4 =