所以,我正在用Python做一个简单的计算器。我最初写的代码是:
from tkinter import *
from tkinter import ttk
root = Tk()
window_width = 200
window_height = 300
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
root.geometry(f {window_width}x{window_height}+{center_x}+{center_y} )
def clear_text():
entry_box.delete(0, END)
def print_one():
entry_box.insert(END, "1")
def print_two():
entry_box.insert(END, "2")
def print_three():
entry_box.insert(END, "3")
def print_four():
entry_box.insert(END, "4")
def print_five():
entry_box.insert(END, "5")
def print_six():
entry_box.insert(END, "6")
def print_seven():
entry_box.insert(END, "7")
def print_eight():
entry_box.insert(END, "8")
def print_nine():
entry_box.insert(END, "9")
def print_zero():
entry_box.insert(END, "0")
def print_plus():
entry_box.insert(END, "+")
def print_minus():
entry_box.insert(END, "-")
def print_divide():
entry_box.insert(END, "/")
def print_multiply():
entry_box.insert(END, "*")
def print_bracket0():
entry_box.insert(END, "(")
def print_bracket1():
entry_box.insert(END, ")")
def print_exponent():
entry_box.insert(END, "**")
def do_maths():
equation = entry_box.get()
evaluate_equation = eval(equation)
entry_box.delete(0, END)
entry_box.insert(0, evaluate_equation)
simple_calculator = Label(root,text="Simple Calculator").place(x=30, y=10)
entry_text = StringVar()
entry_box = ttk.Entry(root, textvariable=entry_text)
one = Button(root, text="1", command=print_one).place(x=30,y=50)
two = Button(root, text="2", command=print_two).place(x=60, y=50)
three = Button(root, text="3", command=print_three).place(x=90, y=50)
four = Button(root, text="4", command=print_four).place(x=30, y=80)
five = Button(root, text="5", command=print_five).place(x=60, y=80)
six = Button(root, text="6", command=print_six).place(x=90, y=80)
seven = Button(root, text="7", command=print_seven).place(x=30, y=110)
eight = Button(root, text="8", command=print_eight).place(x=60, y=110)
nine = Button(root, text="9", command=print_nine).place(x=90, y=110)
zero = Button(root, text="0", command=print_zero).place(x=30, y=140)
plus = Button(root, text="+", command=print_plus).place(x=60, y=140)
minus = Button(root, text="-", command=print_minus).place(x=90, y=140)
divide = Button(root, text="/", command=print_divide).place(x=30, y=170)
multiply = Button(root, text="*", command=print_multiply).place(x=60, y=170)
equals = Button(root, text="=", command=do_maths).place(x=90, y=170)
bracket0 = Button(root, text="(", command=print_bracket0).place(x=30, y=200)
bracket1 = Button(root, text=")", command=print_bracket1).place(x=60, y=200)
exponent = Button(root, text="**", command=print_exponent).place(x=90, y=200)
clear = Button(root, text="Clear", command=clear_text).place(x=30, y=230)
entry_box.place(x=30, y=260)
root.mainloop()
which produces the first image. However, I tried to rewrite the code using a different library, Custom Tkinter. This code:
from tkinter import *
from tkinter import ttk
import customtkinter
root = customtkinter.CTk()
window_width = 200
window_height = 300
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
root.geometry(f {window_width}x{window_height}+{center_x}+{center_y} )
def clear_text():
entry_box.delete(0, END)
def print_one():
entry_box.insert(END, "1")
def print_two():
entry_box.insert(END, "2")
def print_three():
entry_box.insert(END, "3")
def print_four():
entry_box.insert(END, "4")
def print_five():
entry_box.insert(END, "5")
def print_six():
entry_box.insert(END, "6")
def print_seven():
entry_box.insert(END, "7")
def print_eight():
entry_box.insert(END, "8")
def print_nine():
entry_box.insert(END, "9")
def print_zero():
entry_box.insert(END, "0")
def print_plus():
entry_box.insert(END, "+")
def print_minus():
entry_box.insert(END, "-")
def print_divide():
entry_box.insert(END, "/")
def print_multiply():
entry_box.insert(END, "*")
def print_bracket():
entry_box.insert(END, "(")
def print_bracket1():
entry_box.insert(END, ")")
def print_exponent():
entry_box.insert(END, "**")
def equate():
equation = entry_box.get()
evaluate_equation = eval(equation)
entry_box.delete(0, END)
entry_box.insert(0, evaluate_equation)
entry_text = StringVar()
entry_box = ttk.Entry(root, textvariable=entry_text)
#one = Button(root, text="1", command=print_one).place(x=30,y=50)
one = customtkinter.CTkButton(master=root, text="1", command=print_one).place(x=10,y=10)
#two = Button(root, text="2", command=print_two).place(x=60, y=50)
two = customtkinter.CTkButton(master=root, text="2", command=print_two).place(x=160,y=10)
#three = Button(root, text="3", command=print_three).place(x=90, y=50)
three = customtkinter.CTkButton(master=root, text="3", command=print_three).place(x=310,y=10)
#four = Button(root, text="4", command=print_four).place(x=30, y=80)
four = customtkinter.CTkButton(master=root, text="4", command=print_four).place(x=10,y=50)
#five = Button(root, text="5", command=print_five).place(x=60, y=80)
five = customtkinter.CTkButton(master=root, text="5", command=print_five).place(x=160,y=50)
#six = Button(root, text="6", command=print_six).place(x=90, y=80)
six = customtkinter.CTkButton(master=root, text="6", command=print_six).place(x=310,y=50)
#seven = Button(root, text="7", command=print_seven).place(x=30, y=110)
seven = customtkinter.CTkButton(master=root, text="7", command=print_seven).place(x=10,y=90)
#eight = Button(root, text="8", command=print_eight).place(x=60, y=110)
eight = customtkinter.CTkButton(master=root, text="8", command=print_eight).place(x=160,y=90)
#nine = Button(root, text="9", command=print_nine).place(x=90, y=110)
nine = customtkinter.CTkButton(master=root, text="9", command=print_nine).place(x=310,y=90)
#zero = Button(root, text="0", command=print_zero).place(x=30, y=140)
zero = customtkinter.CTkButton(master=root, text="0", command=print_zero).place(x=10,y=130)
#plus = Button(root, text="+", command=print_plus).place(x=60, y=140)
plus = customtkinter.CTkButton(master=root, text="+", command=print_plus).place(x=160, y=130)
#minus = Button(root, text="-", command=print_minus).place(x=90, y=140)
minus = customtkinter.CTkButton(master=root, text="-", command=print_minus).place(x=310, y=130)
#divide = Button(root, text="/", command=print_divide).place(x=30, y=170)
divide = customtkinter.CTkButton(master=root, text="/", command=print_divide).place(x=10, y=170)
#multiply = Button(root, text="*", command=print_multiply).place(x=60, y=170)
multiply = customtkinter.CTkButton(master=root, text="x", command=print_multiply).place(x=160, y=170)
#equals = Button(root, text="=", command=do_maths).place(x=90, y=170)
equals = customtkinter.CTkButton(master=root, text="=", command=equate).place(x=310, y=170)
#bracket = Button(root, text="(", command=print_bracket).place(x=30, y=200)
bracket = customtkinter.CTkButton(master=root, text="(", command=print_bracket).place(x=10, y=210)
#bracket1 = Button(root, text=")", command=print_bracket1).place(x=60, y=200)
bracket1 = customtkinter.CTkButton(master=root, text="(", command=print_bracket1).place(x=160, y=210)
#exponent = Button(root, text="**", command=print_exponent).place(x=90, y=200)
exponent = customtkinter.CTkButton(master=root, text="^^", command=print_exponent).place(x=310, y=210)
#clear = Button(root, text="Clear", command=clear_text).place(x=30, y=230)
clear = customtkinter.CTkButton(master=root, text="Clear", command=clear_text).place(x=160, y=250)
entry_box = Entry(font=( Arial 24 ))
entry_box.place(x=170, y=450)
root.mainloop()
That produces the second image. Basically, I have no idea where I went wrong. Pardon my ignorance, but my code looks okay to me - the only changes to the original are that I ve commented out the original buttons and re-written them using the syntax of Custom Tkinter. However, as you can see in the second image, the buttons for the brackets display only the left bracket "(". Can anyone help?