我目前正在用Python创建一个计算器,当我执行代码时,按钮位于错误的位置
我正在使用Tkinter用Python制作一个图形计算器。我的目标是让我的计算器看起来像这样- 我目前仍在构建我的计算器,因此缺少很多东西,但我决定先进行设计。当我执行我的代码时,我得到这个- 我需要帮忙修理这个。我将把代码粘贴到目前为止,这样您就可以发现任何错误我目前正在用Python创建一个计算器,当我执行代码时,按钮位于错误的位置,python,tkinter,calculator,Python,Tkinter,Calculator,我正在使用Tkinter用Python制作一个图形计算器。我的目标是让我的计算器看起来像这样- 我目前仍在构建我的计算器,因此缺少很多东西,但我决定先进行设计。当我执行我的代码时,我得到这个- 我需要帮忙修理这个。我将把代码粘贴到目前为止,这样您就可以发现任何错误 import sys try: from Tkinter import * import tkMessageBox except ImportError: from tkinter import *
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要获得所需的布局,您的输入
用户输出
必须跨越所有按钮列。这可以通过columnspan
grid选项实现:
user_output.grid(row=0, columnspan=3)
此外,您没有在user\u 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)
给予
您需要什么样的帮助?您是否尝试过简单地调整要放置对象的行和列?您是否尝试过其他选项,如
columnspan
?user\u输出在第0行(显式),第0列(默认)。所有按钮的行号都大于0,因此它们必须位于条目下方。所有按钮的列号都大于0,因此它们必须位于条目的右侧。我需要帮助调整条目框,使其不会将所有按钮推到屏幕的右侧。