Tkinter嵌套框架与网格管理器问题 [英] Tkinter Nested Frames with Grid Manager Issues
问题描述
我正在使用python 2.6及其随附的标准Tkinter重新设计接口的布局.麻烦的是,我注意到了一些奇怪的行为,并且我是Tkinter的新手,所以由于经验不足,这可能只是我所忽略的简单事情.
I am redesigning the layout for a interface using python 2.6 and the standard Tkinter included with it. Trouble is that I am noticing some strange behavior, and I am new to Tkinter so this may just be something simple I am overlooking due to inexperience.
这是一些示例代码,它们说明了如何将框架嵌套在主框架中以获得所需的布局:
Here is some example code of how I am nesting frames inside a master frame in order to get the desired layout:
import Tkinter as tk
root = tk.Tk()
master = tk.Frame(root)
frame = tk.Frame(master).grid(sticky = tk.W)
item1 = "Label One"
L1 = tk.Label(frame, text = item1).grid(sticky = tk.W)
b1 = tk.Button(frame, text = "Button 1").grid(sticky = tk.W)
find = tk.Label(frame, text = "Find").grid(sticky = tk.W)
item = tk.Entry(frame, width = 50).grid(sticky = tk.W)
##item.insert(0,"Enter Item to Search For")
L2 = tk.Label(frame, text = "Label Two").grid(sticky = tk.W)
search = tk.Button(frame, text = "Search").grid(row = 3, column = 0)
frame2 = tk.Frame(master).grid(sticky = tk.W)
list1 = tk.Listbox(frame2, width = 100).grid(sticky = tk.W)
##yscroll = tk.Scrollbar(frame2,orient = tk.VERTICAL,command = list1.yview).grid(row = 5, column = 1, sticky = tk.N + tk.S + tk.W)
yscroll = tk.Scrollbar(frame2, orient = tk.VERTICAL).grid(row = 5, column = 1, sticky = tk.N + tk.S + tk.W)
##list1.configure(yscrollcommand = yscroll.set)
##xscroll = tk.Scrollbar(frame2, orient = tk.HORIZONTAL, command = list1.xview).grid(sticky = tk.W + tk.E)
xscroll = tk.Scrollbar(frame2, orient = tk.HORIZONTAL).grid(sticky = tk.W + tk.E)
##list1.configure(xscrollcommand = xscroll.set)
root.mainloop()
当我尝试在条目小部件中插入默认值时,会发生以下错误,并且在开始嵌套框架之前它起作用:
This following error occurs when I try to insert a default value in the entry widget, and it worked before I started nesting frames:
Traceback (most recent call last):
File "H:\Tkinter\Grid.py", line 17, in <module>
item.insert(0,"Enter Item to Search For")
AttributeError: 'NoneType' object has no attribute 'insert'
示例代码中所有注释掉的行都会出现类似的错误.这些是我需要执行的操作,因为当前滚动条无法正常工作,因为我无法告诉他们滚动时更改列表.
Similiar errors occur on all the commented out lines in the example code.These are actions I need to perfrom because currently the scrollbars are not going to work because I can't tell them to change the list when scrolling.
1.当您像这样嵌套框架时,是否必须以其他方式访问小部件的变量?
1.When you nest frames like this do I have to access the variables of the widgets in a different manner?
2.当我使用嵌套框架,而子级和父级都使用网格时,为什么我的小部件放置仅对应于主控件的行数和列数?如果您查看Y轴滚动条的行,则必须执行master = row = 5,col = 1的操作.我相信它应该是frame2的第0行,col 1,因为该小部件的父项设置为frame2,我也希望frame2也使用网格,但是它似乎使用了主框架网格.
2.When I am using nested frames and both the children and the parent are using a grid, why would my widgets placement correspond to just the master's row and column count? If you look at the line for the Y-axis scroll bar I had to do row = 5, col = 1 of the master. I believe it should be row 0, col 1 of frame2, because the widget has the parent set as frame2 and I wanted frame2 to use a grid as well, but it seems to use the master frames grid.
3.是否可以在不同的帧中显示网格线?
3.Is it possible to display the gridlines in the different frames?
推荐答案
使用Tkinter
时,您不应同时创建对象和grid
.
When using Tkinter
, you shouldn't create an object and grid
it at the same time.
bad = tk.Button(frame).grid() #"bad" is None since .grid returns None
good = tk.Button(frame)
good.grid()
要获取特定网格控件的信息(例如单元格),您可能需要使用 grid_info 方法.
And to get at the information (e.g. cell) for a particular gridded widget, you probably want to use the grid_info method.
这篇关于Tkinter嵌套框架与网格管理器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!