由于tkinter框架小部件不支持滚动,因此我的初学者决定探索使用画布容纳先前填充的框架的可能性。在下面的代码中,我有两个我无法处理的问题:为什么最左边的小部件占用了如此大的空间?为什么该应用会占用所有可用的屏幕空间,甚至会缩短小部件,从而不会显示滚动滑块。所有建议都非常欢迎。
#!/usr/bin/env python3
from tkinter import *
class Calc_Table(Frame):
def __init__(self, root, cols, rows):
Frame.__init__(self, master=root)
self.cols = cols
self.rows = rows
hscrollbar = Scrollbar(orient=HORIZONTAL)
hscrollbar.grid(row = 1, column = 0, sticky = EW)
vscrollbar = Scrollbar(orient=VERTICAL)
vscrollbar.grid(row = 0, column = 1, sticky = NS)
self.canvas = Canvas(self, xscrollcommand = hscrollbar.set, yscrollcommand = vscrollbar.set,bg='yellow')
self.canvas.grid(row = 0, column = 0, sticky = NSEW)
self.canvas.grid_rowconfigure(0, weight = 1)
self.canvas.grid_columnconfigure(0, weight = 1)
hscrollbar.config(command = self.canvas.xview)
vscrollbar.config(command = self.canvas.yview)
self.initial_table = self.create_table(self.cols,self.rows)
self.canvas_frame = self.canvas.create_window(0, 0, window = self.initial_table,
width=400, height=200, anchor=NW)
self.canvas.update_idletasks()
self.canvas.config(scrollregion = self.canvas.bbox('all'))
def create_table(self, cols, rows): # build initial table ---->
self.cols, self.rows = cols, rows
for c in range(self.cols):
for r in range(self.rows):
if c == 0 and r == 0:
self.lbl = Label(self, width=6, height=1, bg="white",
text="? ? ?", relief=RAISED)
self.lbl.grid(column=c, row=r, padx=1, pady=1, sticky=NSEW)
elif c == 0 and r != 0:
self.lbl = Label(self, width=6, height=1, bg="white",
text=f"R{r}",relief=RAISED)
self.lbl.grid(column=c, row=r, padx=1, pady=1, sticky=NSEW)
elif c != 0 and r == 0:
self.lbl = Label(self, width=6, height=1, bg="white",
text=f"C{c}",relief=RAISED)
self.lbl.grid(column=c, row=r, padx=1, pady=1, sticky=NSEW)
else:
self.txt =Text(self, width=6, height=1, bg="white",
relief=SUNKEN)
self.txt.grid(column=c, row=r, padx=1, pady=1, sticky=NSEW)
for c in range(self.cols):
self.grid_columnconfigure(c, weight=1)
for r in range(self.rows):
self.grid_rowconfigure(r, weight=1)
if __name__ == '__main__':
cols = int(input("How many columns? "))
assert isinstance(int(cols), int), "integers only, please"
rows = int(input("How many rows? "))
assert isinstance(int(rows), int), "integers only, please"
root = Tk()
root.geometry("+10+10")
root.title("Master Table")
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
my_table = Calc_Table(root, cols, rows)
my_table.grid(column=0, row=0, sticky=NSEW)
my_table.grid_columnconfigure(0, weight=1)
my_table.grid_rowconfigure(0, weight=1)
root.mainloop()
请在下面找到经过修改的代码,但是画布中的框架和小部件仍然无法展开:
from tkinter import *
class Calc_Table(Frame):
def __init__(self, root, cols, rows):
Frame.__init__(self, master=root)
self.cols = cols
self.rows = rows
hscrollbar = Scrollbar(orient=HORIZONTAL)
hscrollbar.grid(row = 1, column = 0, sticky = EW)
vscrollbar = Scrollbar(orient=VERTICAL)
vscrollbar.grid(row = 0, column = 1, sticky = NS)
self.canvas = Canvas(self, xscrollcommand = hscrollbar.set, yscrollcommand = vscrollbar.set, bg='yellow')
self.canvas.grid(row = 0, column = 0, sticky = NSEW)
self.canvas.grid_rowconfigure(0, weight = 1)
self.canvas.grid_columnconfigure(0, weight = 1)
hscrollbar.config(command = self.canvas.xview)
vscrollbar.config(command = self.canvas.yview)
self.table_frame = self.create_table(self.cols,self.rows)
self.canvas_frame = self.canvas.create_window(0, 0, window = self.table_frame, anchor=NW)
self.canvas.update_idletasks()
self.canvas.config(scrollregion = self.canvas.bbox('all'))
print(self.canvas.find_all()) #check canvas slaves
print(len(self.table_frame.grid_slaves())) #check table_frame slaves
def create_table(self, cols, rows): # build initial table ---->
self.table_frame = Frame(self.canvas)
self.table_frame.grid(column = 0, row = 0, sticky = NSEW)
self.cols, self.rows = cols, rows
for c in range(self.cols):
for r in range(self.rows):
if c == 0 and r == 0:
self.lbl = Label(self.table_frame, width=6, height=1, bg="white",text="? ? ?", relief=RAISED)
self.lbl.grid(column=c, row=r, padx=1, pady=1,sticky=NSEW)
elif c == 0 and r != 0:
self.lbl = Label(self.table_frame, width=6, height=1, bg="white", text=f"R{r}",relief=RAISED)
self.lbl.grid(column=c, row=r, padx=1, pady=1,sticky=NSEW)
elif c != 0 and r == 0:
self.lbl = Label(self.table_frame, width=6, height=1, bg="white",text=f"C{c}",relief=RAISED)
self.lbl.grid(column=c, row=r, padx=1, pady=1,sticky=NSEW)
else:
self.txt =Text(self.table_frame, width=6, height=1, bg="white", relief=SUNKEN)
self.txt.grid(column=c, row=r, padx=1, pady=1,sticky=NSEW)
self.table_frame.grid_columnconfigure(0, weight=1)
self.table_frame.grid_rowconfigure(0, weight=1)
for c in range(self.cols):
self.table_frame.grid_columnconfigure(c, weight=1)
for r in range(self.rows):
self.table_frame.grid_rowconfigure(r, weight=1) # end table
return self.table_frame
if __name__ == '__main__':
cols = int(input("How many columns? "))
assert isinstance(int(cols), int), "integers only, please"
rows = int(input("How many rows? "))
assert isinstance(int(rows), int), "integers only, please"
root = Tk()
root.geometry("+10+10")
root.title("Master Table")
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
my_table = Calc_Table(root, cols, rows)
my_table.configure(bg = "green")
my_table.grid(column=0, row=0, sticky=NSEW)
my_table.grid_columnconfigure(0, weight=1)
my_table.grid_rowconfigure(0, weight=1)
root.mainloop()
答案 0 :(得分:0)
为什么最左上角的小部件占用如此大的空间
主要原因是因为画布位于self
的第0行,第零列中,并且画布的默认大小约为200像素。如果更改画布的高度,您将看到行的大小更改相同的数量。
另一个问题是,您在create_table
中创建的所有小部件都被赋予self
作为主窗口。如果要创建可滚动表,则这些表必须是画布内框架的子代。
为什么应用会占用所有可用的屏幕空间,甚至会缩短小部件,从而不显示滚动滑块。
由于与以前相同的原因-create_table
将小部件放在根窗口中,并将它们与滚动条放在同一行和同一列中。
您做错的基本事情是没有在画布内创建框架。您需要在create_table
中创建一个框架,将所有其他小部件放在该框架中的create_table
中,然后确保create_table
返回此框架。
def create_table(self, cols, rows):
table_frame = Frame(self.canvas)
...
self.lbl = Label(table_frame, ...)
...
return table_frame