我正在使用网格几何图形,我需要在最大化窗口时仅扩展画布。画布位于第3行和第0列的框架上。 当我做 master.grid_rowconfigure(3,weight = 1) self.canvasFrame.grid_rowconfigure(0,weight = 1) 然后画布在行上展开,但我也需要在列上展开。 所以我做了 master.grid_columnconfigure(0,weight = 1) self.canvasFrame.grid_columnconfigure(0,weight = 1)
,但它也扩展了行#1的列#0。我不要那个。
这是我的代码。
使用网格几何管理器是否可以实现?
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
class MainUI:
def __init__(self,master):
self.master = master
# row 1
ttk.Label(self.master, text="Customer").grid(row=0, column=0, sticky=W, pady = 5, padx = 15)
ttk.Label(self.master, text="Items").grid(row=0,column=1, sticky=W, pady = 5, padx = 15)
ttk.Label(self.master, text="Price").grid(row=0,column=2, sticky=W, pady = 5, padx = 15)
ttk.Label(self.master, text="Cost").grid(row=0,column=3, sticky=W, pady = 5, padx = 15)
# row 2
self.customerCombo = ttk.Combobox( self.master,
values=[])
self.customerCombo.grid(row=1,column=0, sticky=W, pady = 5, padx = 15)
self.jobCombo = ttk.Combobox( self.master,
values=[])
self.jobCombo.grid(row=1,column=1, sticky=W, pady = 5, padx = 15)
self.estLaborEntry = ttk.Entry(self.master).grid(row=1,column=2, sticky=W, pady = 5, padx = 15)
self.estMaterialEntry = ttk.Entry(self.master).grid(row=1,column=3, sticky=W, pady = 5, padx = 15)
# row 3
self.addRowButton = ttk.Button(self.master, command=self.show, text="Add Row").grid(row=2, column = 2, sticky=W, pady = 5, padx = 15)
self.addRowButton = ttk.Button(self.master, command=self.show, text="Add Col").grid(row=2, column = 3, sticky=W, pady = 5, padx = 15)
#row 4
self.canvasFrame = Frame(master)
self.canvasFrame.grid(row=3, column=0, columnspan=3, sticky=W+E+N+S, pady=5, padx=15)
master.grid_rowconfigure(3,weight=1)
master.grid_columnconfigure(0,weight=1)
xscrollbar = Scrollbar(self.canvasFrame, orient=HORIZONTAL)
xscrollbar.grid(row=1, column=0, sticky=E+W)
yscrollbar = Scrollbar(self.canvasFrame)
yscrollbar.grid(row=0, column=1, sticky=N+S)
canvas = Canvas(self.canvasFrame, bd=0,
scrollregion=(0, 0, 1000, 1000),
xscrollcommand=xscrollbar.set,
yscrollcommand=yscrollbar.set)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
self.canvasFrame.grid_rowconfigure(0,weight=1)
self.canvasFrame.grid_columnconfigure(0,weight=1)
def show(self):
messagebox.showinfo("title", "message")
def on_closing():
root.destroy()
# from tkinter import messagebox
# if messagebox.askokcancel("Quit", "Do you want to quit?"):
# root.destroy()
root = Tk()
mainui = MainUI(root)
root.title("Test")
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()