我的目标是在启动时有一个提示,并根据输入的整数,将许多画布放在框中。框架必须有一个固定的高度(Canvases也是如此),但宽度应根据窗口的大小而变化,并在画布中平均分配。仅调整不适合使用包的画布宽度()
这适用于最多4个画布,之后画布也不适合最大窗口。
另外,为什么我无法在画布上下看到20像素的空灰框,因为画布的高度比框的高度还要小?
from tkinter import *
from tkinter import simpledialog
b=[]
root = Tk()
no_of_players=simpledialog.askinteger(prompt="Enter here", title="No of participants")
status_frame=Frame(root, bg='gray', height=100)
status_frame.pack(fill=X)
for i in range(no_of_players):
c=Canvas(status_frame, bg="orange")
b.append(c)
b[i].pack(side=LEFT,fill=X, expand=True)
root.mainloop()
编辑
from tkinter import *
from tkinter import simpledialog
b=[]
root = Tk()
no_of_players=simpledialog.askinteger(prompt="Enter here", title="No of participants")
status_frame=Frame(root, bg='gray', height=500)
status_frame.pack(fill=X)
for i in range(no_of_players):
c=Canvas(status_frame, width=1, height=100, bg="orange")
b.append(c)
b[i].pack(side=LEFT,fill=X, expand=True)
root.mainloop()
我不明白你的问题。你不会设置画布宽度,所以它默认为某些东西(我的机器上是378像素)。你认为它没有做什么? – Novel
@Jonathan在第二个[asnwer]中建议的东西就像这样(http://stackoverflow.com/questions/22835289/how-to-get-tkinter-canvas-to-dynamically- resize-to-window-width)。我基本上希望画布的数量能够在X方向上完全填充框架,即基于画布的数量,即使在调整窗口大小时也应该在它们之间划分可用的框架宽度。 –
我建议你暂时给'status_frame'一个独特的颜色,以便你能看到它的边缘在哪里。我的猜测是,他们不在你认为的地方。 –