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.When you nest frames like this do I have to access the variables of the widgets in a different manner?
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.Is it possible to display the gridlines in the different frames?
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()
And to get at the information (e.g. cell) for a particular gridded widget, you probably want to use the grid_info method.
来源:https://stackoverflow.com/questions/11616038/tkinter-nested-frames-with-grid-manager-issues