我正在用Python和Tkinter做一个小项目。我需要展示一张桌子,上面有一些挖掘机/挖土机的所有特性。在
表格基本上是一个大框架,里面有很多标签对象。我把这个框架放在一个Canvas对象中(使用create_window()
),它位于另一个frame(与滚动条一起)。在
问题是,我不能正确地将表格形象化;我只能得到它的垂直部分:
绿色部分是包含Canvas对象和滚动条的框架
我发现解决这个问题的唯一方法是使用固定宽度…但这不是很好。 有什么建议吗?在
编辑:
我希望桌子有一个固定的高度(并且这是有效的),正如您在这里看到的:
红色部分是canvas对象,它包含表,绿色部分是frame,它包含画布和滚动条。我删除了滚动条,这样更容易看到发生了什么。在
问题是canvas对象没有展开(请参阅前面的屏幕截图),我不知道原因。 我希望画布的宽度与表的宽度一致。在
代码在这里:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Tkinter, string, table
from functions import *
bg_color = "#d3d3d3"
first_table = True
# MODEL: Functions to handle the main processing of the program in
# response to user interaction
def create_tables():
global tab, first_table, tab_space, mbtext
select = choice.get()
choices = [ 1, 2, 3, 4, 5]
mbtext.set( str( choices[select])) # resetting the number in the menubutton
if not first_table:
# cleaning the canvas
t.destroy()
comp_space.destroy()
# space to display the objects
master = Tkinter.Frame( top, bg = 'green')
master.grid( row = 2, column = 0, sticky = "wesn", padx = 10 )
# space for the table:
tab_space = Tkinter.Canvas( master, bg = 'red', highlightthickness = 0, height = 600, scrollregion=(0, 0, 2000, 780))
tab_space.grid( row = 0, column = 0)
# creating the table...
tab = table.CompTable( tab_space, columns = choices[select]+1 )
tab.first_set()
tab_space.create_window(0, 0, anchor = "nw", window = tab)
# and the scrollbar:
scrollY = Tkinter.Scrollbar ( master, bg = bg_color, bd = 4, activebackground = bg_color, orient = "vertical")
scrollY.grid( row = 0, column = 1, sticky = "nswe")
#binding canvas and scrollbar together
scrollY.configure( command = tab_space.yview)
tab_space.configure(yscrollcommand = scrollY.set )
# VIEW: Setup the widgets
# The main window
top = Tkinter.Tk()
top.configure( bg = bg_color)
top.title("Comparatore")
# logo_frame/canvas - using a Canvas object to load images
logo_canvas = Tkinter.Canvas( top, bg = bg_color, highlightthickness = 0, height = 58, width = 590 )
logo_canvas.grid( row = 0, column = 0, ipadx = 0, ipady=0, sticky = "nw")
logo = Tkinter.PhotoImage(file = "Images/Logo.gif")
logo_canvas.create_image(0, 0, image = logo, anchor="nw")
# background
bg_label = Tkinter.Label( top, bg = bg_color )
bg_label.grid( row = 1, column = 0, sticky = "nesw")
# menu to handle how many items we are using
select_text = Tkinter.Label( bg_label, text = " Selezionare il numero di macchine da confrontare: ",
font = ("verdana", 16), bg = bg_color)
select_text.grid( row = 0, column = 0, sticky = "nsew")
mbtext = Tkinter.StringVar()
mbtext.set("")
how_many_mb = Tkinter.Menubutton( bg_label, textvariable = mbtext, relief= "raised", bg = bg_color)
how_many_mb.menu = Tkinter.Menu( how_many_mb, tearoff = 0)
how_many_mb["menu"] = how_many_mb.menu
how_many_mb.grid( row = 0, column = 1, sticky = "nsew", padx = 4, ipadx = 18)
# CONTROLLER
choice = Tkinter.IntVar()
how_many_mb.menu.add_radiobutton( label = "1", variable = choice, value = 0, command = create_tables)
how_many_mb.menu.add_radiobutton( label = "2", variable = choice, value = 1, command = create_tables)
how_many_mb.menu.add_radiobutton( label = "3", variable = choice, value = 2, command = create_tables)
how_many_mb.menu.add_radiobutton( label = "4", variable = choice, value = 3, command = create_tables)
how_many_mb.menu.add_radiobutton( label = "5", variable = choice, value = 4, command = create_tables)
##
Tkinter.mainloop()
这是表格模块的代码:
^{pr2}$
尝试将此添加到表中,在
tab_space.grid( row = 0, column = 0)
之后创建代码:编辑
好吧,这或多或少解决了上述问题:
如果你更换
^{pr2}$与
说明:
Tkinter
grid
几何管理器对小部件中的每一列和每一行都有一个“权重”。列/行的权重决定了该列或行占小部件分配的空间的多少。在您可以更改给定列/行的权重。例如,如果有一个名为
frame
的框架,那么frame.columnconfigure(3, weight=7)
将frame
小部件的第3列的权重设置为7。有一个对应的函数叫做rowconfigure
。在默认权重为0,这意味着列/行不会随着可用空间或所需空间的增加或减少而增长或收缩-它们是静态的。如果列0的权重为2,列1的权重为3,并且有5个额外的像素可用,则列0扩展2个像素,列1扩展3个像素。在
如果只有一个列,最后一点就无关紧要了,但是这个问题的重要部分是,包含表画布的列(以及包含主框架的列)具有一个非零权重,因此网格几何管理器知道允许它完全展开或收缩,而不是保持它的任何宽度最初被分配。在
来自NMT的}函数的Here is some documentation。关注点:}的别名,我假设,实现它可以节省5次击键。在
columnconfigure
和{columnconfigure
和rowconfigure
实际上是grid_columnconfigure
和{注:
通过手动调整窗口大小,您应该能够看到表的其余部分。我想您需要使用
<Configure>
事件来让窗口自动调整到正确的宽度。在相关问题 更多 >
编程相关推荐