English 中文(简体)
自定义Tkinter按钮显示错误的字符
原标题:Custom Tkinter button displaying the wrong character

所以,我正在用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?

一个简单的Tkinter计算器的图像

一个简单的自定义Tkinter计算器的图像

问题回答

在您的第二块代码中,您写道:

bracket1 = customtkinter.CTkButton(master=root, text="(", command=print_bracket1).place(x=160, y=210)

这是为了显示“)”符号。但是,您设置

text="("

This would set the symbol to "(" instead of ")". If you look at your original block of code, you set it to the correct symbol. This is why there is a difference.

我建议将window_widthwindows_height设置为全大写。它应该看起来像这个WINOW_WIDTH和这个WINOW_HEIGHT。这样做的原因是为了表明这些变量是常数,这意味着它们永远不会改变,并且总是常数。这在Python样式指南。还有其他部分可以提高可读性,但我不担心前几个项目中的可读性或样式指南。你所需要做的就是习惯语法并解决问题。如果你需要更多解释,请告诉我!





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签