我正在Windows 8计算机上用Python 3创建GUI。 GUI是围绕画布的水平和垂直滚动条,所有这些都位于一个框架中。我想在窗口大小改变时调整内框的内容(水平展开或收缩)。
问题:我有一个工作水平滚动条,或者我的内框的内容可以调整大小。我似乎无法兼顾两者。
代码:
from tkinter import *
from tkinter import filedialog
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
import subprocess
import datetime
import os
# Vertical padding for all objects, and Horizontal padding for all objects (if ever used).
v_pad, h_pad = 2, 2
# Set the 'x' and 'y' location of the top left corner of the GUI.
x, y = 0, 0
w, h = 500, 500
class Application(Frame):
# Initialize the Frame
def __init__(self, master):
# Method to get canvas and contents to resize properly
def onCanvasConfigure(self):
[ts_frame.columnconfigure(x, minsize="3cm", weight=1) for x in [1, 2, 3, 4, 5]]
c1.itemconfigure("innerFrame", width=c1.winfo_width())
c1.configure(scrollregion=c1.bbox("all"))
Frame.__init__(self, master)
nbook = ttk.Notebook(root)
nbook.pack(fill='both', expand='yes')
f1 = ttk.Frame(nbook)
c1 = Canvas(f1, bg='green', bd=0, highlightthickness=0)
ts_frame = ttk.Frame(c1)
vsbar1 = Scrollbar(f1, orient="vertical", command=c1.yview)
hsbar1 = Scrollbar(f1, orient="horizontal", command=c1.xview)
vsbar1.pack(side="right", fill="y")
hsbar1.pack(side="bottom", fill="x")
c1.config(yscrollcommand=vsbar1.set, xscrollcommand=hsbar1.set)
c1.pack(side="left", fill="both", expand=True)
c1.create_window(0, 0, window=ts_frame, anchor="nw", tags=("innerFrame",))
#c1.bind("<Configure>", lambda event: c1.configure(scrollregion=c1.bbox("all")), [ts_frame.columnconfigure(x, minsize="2cm", weight=1) for x in [1, 2, 3, 4, 5]])
c1.bind("<Configure>", onCanvasConfigure)
self.ts_tab(ts_frame)
nbook.add(f1, text='Time Stamp Check')
# Start ts_tab
def ts_tab(self, tab_loc):
# Set up file name entry.
Label(tab_loc, text="Select file:").grid(row=0, column=0, sticky=W)
self.flnm1 = ttk.Entry(tab_loc)
self.flnm1.focus_set()
self.flnm1.grid(row=0, column=1, columnspan=4, sticky=EW)
ttk.Button(tab_loc, text="Browse...", width=10, command=lambda: self.browse(self.flnm1)).\
grid(row=0, column=5)
def browse(self, target):
temp = filedialog.askopenfilename()
target.delete(0, END)
target.insert(0, temp)
root = Tk()
root.iconbitmap("gws_logo_new.png")
# Create the menu bar
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label='Open')
filemenu.add_command(label='Save As')
filemenu.add_command(label='Quit', command=root.quit)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label='Read Me')
# put the 'File' and 'Help' dropdown menus into the menubar.
menubar.add_cascade(label='File', menu=filemenu)
menubar.add_cascade(label='Help', menu=helpmenu)
root.config(menu=menubar)
root.geometry("%ix%i+%i+%i" % (w, h, x, y))
app = Application(root)
root.mainloop()
我认为问题必须处理我的方法onCanvasConfigure
# Method to get canvas and contents to resize properly
def onCanvasConfigure(self):
[ts_frame.columnconfigure(x, minsize="3cm", weight=1) for x in [1, 2, 3, 4, 5]]
c1.itemconfigure("innerFrame", width=c1.winfo_width())
c1.configure(scrollregion=c1.bbox("all"))
,在我的内框columnconfigure
上执行ts_frame
。如果没有columnconfigure
,水平滚动条可以正常工作,但不会调整ts_frame
内容的大小。
为了兼顾两者,我需要做什么?
编辑 - 根据布莱恩·奥克利回答
将方法onCanvasConfigure
修改为以下
def onCanvasConfigure(event):
wdth = max(event.width, ts_frame.winfo_reqwidth())
hght = max(event.height, ts_frame.winfo_reqheight())
c1.itemconfigure("innerFrame", width=wdth, height=hght)
c1.configure(scrollregion=c1.bbox("all"))
[ts_frame.columnconfigure(x, minsize="3cm", weight=1) for x in [1, 2, 3, 4, 5]]
通过此修改,当调整窗口大小时,滚动条起作用,内框和输入框展开/收缩。
答案 0 :(得分:1)
如果希望内部框架随着画布增长和缩小,可以使用canvas itemconfigure
命令设置其实际宽度和高度。
要使滚动条工作和要调整框架的大小,您必须确定框架想要的大小,确定画布的大小,然后配置框架以使其具有宽度和高度值更大。您可以使用winfo_reqwidth
和winfo_reqheight
获取所请求的框架宽度和高度。
以下是一个例子:
def onCanvasConfigure(event):
width = max(event.width, ts_frame.winfo_reqwidth())
height = max(event.height, ts_frame.winfo_reqheight())
c1.itemconfigure("innerFrame", width=width, height=height)
c1.configure(scrollregion=c1.bbox("all"))