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

时间:2018-07-14 15:10:14

标签: python tkinter calculator

我正在使用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()

如果有人可以帮助我,我会很高兴的:)

-CodeExecution

1 个答案:

答案 0 :(得分:0)

要获取所需的布局,您的输入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)

给予

screenshot