Pythons .grid() 函数如何工作
How does Pythons .grid() function work
我尝试使用以下方法来对齐按钮,但它不起作用
#creates entry inputs for answers
entry=Entry(master)
entry.pack()
entry.focus_set()
#button 1 created and used to call addition function
b1 = Button(master, text="+", command=callback)
b1.pack(side=LEFT)
#button 2 created and used to call subtraction function
b2 = Button(master, text="-", command=callback2)
b2.pack(side=LEFT)
#button 3 created and used to call divison function
b3 = Button(master, text="/", command=callback3)
b3.pack(side=LEFT)
#button 4 created and used to call multiplication function
b4 = Button(master, text="*", command=callback4)
b4.pack(side=LEFT)
#button 5 created and used to check answers
b5 = Button(master, text="Check Ans", width=10, command=callbackinput)
b5.pack(side=LEFT)
编辑 我正在尝试让一个条目位于左对齐的四个按钮上方居中
到目前为止我得到的(image)需要对齐按钮上方的条目
同样,当使用 .grid(row= , column=) 时,GUI 不会出现
您的代码中的问题是您同时调用了 grid
和 pack
。 Tkinter 有三个几何管理器:这两个,加上 place
。一个小部件一次只能由其中一个控制。当您调用多个时,只有最后一个有效。因此,当您调用 pack
.
时,调用 grid
的任何效果都将无效
由于您使用 pack
作为您的几何管理器,当您在其中一个按钮上调用 grid
时,tkinter 不知道该怎么做。选择一个几何管理器并在整个 window.
中坚持使用它
from tkinter import *
master = Tk()
# some dummy callback functions
callback, callback2, callback3, callback4, callbackinput = [lambda: None]*5
#creates entry inputs for answers
entry=Entry(master)
entry.grid(row=0, column=0, columnspan=6)
entry.focus_set()
#button 1 created and used to call addition function
b1 = Button(master, text="+", command=callback)
b1.grid(row=1, column=0)
#button 2 created and used to call subtraction function
b2 = Button(master, text="-", command=callback2)
b2.grid(row=1, column=1)
#button 3 created and used to call divison function
b3 = Button(master, text="/", command=callback3)
b3.grid(row=1, column=2)
#button 4 created and used to call multiplication function
b4 = Button(master, text="*", command=callback4)
b4.grid(row=1, column=3)
#button 5 created and used to check answers
b5 = Button(master, text="Check Ans", width=10, command=callbackinput)
b5.grid(row=1, column=4, columnspan=2)
创建一个 window 看起来像:
我尝试使用以下方法来对齐按钮,但它不起作用
#creates entry inputs for answers
entry=Entry(master)
entry.pack()
entry.focus_set()
#button 1 created and used to call addition function
b1 = Button(master, text="+", command=callback)
b1.pack(side=LEFT)
#button 2 created and used to call subtraction function
b2 = Button(master, text="-", command=callback2)
b2.pack(side=LEFT)
#button 3 created and used to call divison function
b3 = Button(master, text="/", command=callback3)
b3.pack(side=LEFT)
#button 4 created and used to call multiplication function
b4 = Button(master, text="*", command=callback4)
b4.pack(side=LEFT)
#button 5 created and used to check answers
b5 = Button(master, text="Check Ans", width=10, command=callbackinput)
b5.pack(side=LEFT)
编辑 我正在尝试让一个条目位于左对齐的四个按钮上方居中
到目前为止我得到的(image)需要对齐按钮上方的条目
同样,当使用 .grid(row= , column=) 时,GUI 不会出现
您的代码中的问题是您同时调用了 grid
和 pack
。 Tkinter 有三个几何管理器:这两个,加上 place
。一个小部件一次只能由其中一个控制。当您调用多个时,只有最后一个有效。因此,当您调用 pack
.
grid
的任何效果都将无效
由于您使用 pack
作为您的几何管理器,当您在其中一个按钮上调用 grid
时,tkinter 不知道该怎么做。选择一个几何管理器并在整个 window.
from tkinter import *
master = Tk()
# some dummy callback functions
callback, callback2, callback3, callback4, callbackinput = [lambda: None]*5
#creates entry inputs for answers
entry=Entry(master)
entry.grid(row=0, column=0, columnspan=6)
entry.focus_set()
#button 1 created and used to call addition function
b1 = Button(master, text="+", command=callback)
b1.grid(row=1, column=0)
#button 2 created and used to call subtraction function
b2 = Button(master, text="-", command=callback2)
b2.grid(row=1, column=1)
#button 3 created and used to call divison function
b3 = Button(master, text="/", command=callback3)
b3.grid(row=1, column=2)
#button 4 created and used to call multiplication function
b4 = Button(master, text="*", command=callback4)
b4.grid(row=1, column=3)
#button 5 created and used to check answers
b5 = Button(master, text="Check Ans", width=10, command=callbackinput)
b5.grid(row=1, column=4, columnspan=2)
创建一个 window 看起来像: