我正在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]]通过这种修改,滚动条可以工作,当窗口调整大小时,内部框架和输入框展开/收缩。
发布于 2015-12-10 17:57:51
如果希望内部框架与画布一起生长和收缩,则可以使用画布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"))https://stackoverflow.com/questions/34206986
复制相似问题