我目前正在 Python 中创建一个计算器,当我执行代码时,按钮位于错误的位置

I am currently creating a calculator in Python and when I execute code, the buttons are in the wrong place

我正在使用 Tkinter 在 Python 中制作图形计算器。我的目标是让我的计算器看起来像这样 - http://prntscr.com/k6exeu

我目前仍在构建我的计算器,因此缺少很多东西,但我决定先进行设计。当我执行我的代码时,我得到这个 - http://prntscr.com/k6eyhm

我需要帮助解决这个问题。我会粘贴到目前为止的代码,以便您发现任何错误。

import sys

try:
    from Tkinter import *
    import tkMessageBox

except ImportError:
    from tkinter import *
    from tkinter import messagebox

window = Tk()
window.title("PyCalc")
window.geometry("500x500")

user_output = Entry(width=50, state='readonly', justify=CENTER)
zero = Button(text="0", height=3, width=3, justify=LEFT)
one = Button(text="1", height=3, width=3, justify=LEFT)
two = Button(text="2", height=3, width=3, justify=LEFT)
three = Button(text="3", height=3, width=3, justify=LEFT)
four = Button(text="4", height=3, width=3, justify=LEFT)
five = Button(text="5", height=3, width=3, justify=LEFT)
six = Button(text="6", height=3, width=3, justify=LEFT)
seven = Button(text="7", height=3, width=3, justify=LEFT)
eight = Button(text="8", height=3, width=3, justify=LEFT)
nine = Button(text="9", height=3, width=3, justify=LEFT)

user_output.grid(row=0)
zero.grid(row=4, column=3, sticky=N+S+E+W)
one.grid(row=1, column=1, sticky=N+S+E+W)
two.grid(row=1, column=2, sticky=N+S+E+W)
three.grid(row=1, column=3, sticky=N+S+E+W)
four.grid(row=2, column=1, sticky=N+S+E+W)
five.grid(row=2, column=2, sticky=N+S+E+W)
six.grid(row=2, column=3, sticky=N+S+E+W)
seven.grid(row=3, column=1, sticky=N+S+E+W)
eight.grid(row=3, column=2, sticky=N+S+E+W)
nine.grid(row=3, column=3, sticky=N+S+E+W)

window.mainloop()

如果有人愿意帮助我,我会很高兴:)

-代码执行

要获得您想要的布局,您的条目 user_output 必须跨越所有按钮列。这可以通过 columnspan 网格选项来实现:

user_output.grid(row=0, columnspan=3)

此外,您没有在 user_output.grid 中指定列,因此默认情况下它位于第 0 列,而您最左边的按钮位于第 1 列。

user_output.grid(row=0, columnspan=3)
zero.grid(row=4, column=2, sticky=N+S+E+W)
one.grid(row=1, column=0, sticky=N+S+E+W)
two.grid(row=1, column=1, sticky=N+S+E+W)
three.grid(row=1, column=2, sticky=N+S+E+W)
four.grid(row=2, column=0, sticky=N+S+E+W)
five.grid(row=2, column=1, sticky=N+S+E+W)
six.grid(row=2, column=2, sticky=N+S+E+W)
seven.grid(row=3, column=0, sticky=N+S+E+W)
eight.grid(row=3, column=1, sticky=N+S+E+W)
nine.grid(row=3, column=2, sticky=N+S+E+W)

给予