在Tkinter计算器中显示减号和加号

时间:2018-08-05 17:38:57

标签: python tkinter

现在我正在用python和tkinter制作计算器,但是我有一个小问题。减号和加号以一种非常奇怪的方式移动,我认为它看起来并不酷。Look at Entry in which calculation should happend

所以,我只想知道如何将它们居中(移动减去更高一点)。

我的代码:

from tkinter import *

root = Tk()
root.title("Calculator")
root.resizable(width=False, height=False)

# Main variables
equation = StringVar()
equation.set("0")

equa=""

# Program functions
def ButtonPress(num):
   global equa
   equa = equa + str(num)
   equation.set(equa)

# Widgets
calculation = Entry(root, textvariable=equation, font=("Arial", 15,"bold"), 
bd=12, insertwidth=4, width=15)
calculation.grid(columnspan=4)
# Numbers
button1 = Button(root, text='1', command=lambda: ButtonPress(1), bg="gainsboro",bd=3,  padx=12, pady=5, font=("Arial", 13, "bold"))
button1.grid(row=1, column=0, sticky=W)
button2 = Button(root, text='2', command=lambda:ButtonPress(2),bg="gainsboro",bd=3,  padx=12, pady=5, font=("Arial", 13, "bold"))
button2.grid(row=1, column=1, sticky=W)
button3 = Button(root, text='3', command=lambda: ButtonPress(3),bg="gainsboro",bd=3,  padx=12, pady=5, font=("Arial", 13, "bold"))
button3.grid(row=1, column=2, sticky=W)
button4 = Button(root, text='4', command=lambda: ButtonPress(4),bg="gainsboro",bd=3,  padx=12, pady=5, font=("Arial", 13, "bold"))
button4.grid(row=2, column=0, sticky=W)
button5 = Button(root, text='5', command=lambda: ButtonPress(5),bg="gainsboro",bd=3,  padx=12, pady=5, font=("Arial", 13, "bold"))
button5.grid(row=2, column=1, sticky=W)
button6 = Button(root, text='6', command=lambda: ButtonPress(6),bg="gainsboro",bd=3,  padx=12, pady=5, font=("Arial", 13, "bold"))
button6.grid(row=2, column=2, sticky=W)
button7 = Button(root, text='7', command=lambda: ButtonPress(7),bg="gainsboro",bd=3,  padx=12, pady=5, font=("Arial", 13, "bold"))
button7.grid(row=3, column=0, sticky=W)
button8 = Button(root, text='8', command=lambda: ButtonPress(8),bg="gainsboro",bd=3,  padx=12, pady=5, font=("Arial", 13, "bold"))
button8.grid(row=3, column=1, sticky=W)
button9 = Button(root, text='9', command=lambda: ButtonPress(9),bg="gainsboro",bd=3,  padx=12, pady=5, font=("Arial", 13, "bold"))
button9.grid(row=3, column=2, sticky=W)
button0 = Button(root, text='0', command=lambda: ButtonPress(0),bg="gainsboro",bd=3,  padx=12, pady=5, font=("Arial", 13, "bold"))
button0.grid(row=4, column=1, sticky=W)
button_plus = Button(root, text='+', command=lambda: ButtonPress('+'),bg="gainsboro",bd=3,  padx=12, pady=5, font=("Arial", 13, "bold"))
button_plus.grid(row=1, column=3, sticky=W)
button_minus = Button(root, text='-', command=lambda: ButtonPress('-'),bg="gainsboro",bd=3,  padx=14, pady=5, font=("Arial", 13, "bold"))
button_minus.grid(row=2, column=3, sticky=W)



root.mainloop()

1 个答案:

答案 0 :(得分:0)

最简单的解决方案是选择等宽字体,例如您可以尝试:

public class TstFrag extends Fragment {

public TstFrag() {

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.layout_cht, container, false);
}}