为什么我的水平滚动条在使用columnconfigu后停用

2024-04-24 20:12:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在windows8计算机上用python3创建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。如果没有columnconfigure,水平滚动条可以正常工作,但是ts_frame内容的大小不能调整。你知道吗

我需要做什么才能两者兼得?你知道吗

编辑-根据Bryan Oakley回答

将方法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]]

通过这种修改,滚动条工作,当窗口调整大小时,内部框架和输入框展开/收缩。你知道吗


Tags: theimportselfaddforrootallwidth
1条回答
网友
1楼 · 发布于 2024-04-24 20:12:54

如果您希望内部框架随画布一起增长和收缩,可以使用canvasitemconfigure命令来设置其实际宽度和高度。你知道吗

要使滚动条工作并调整帧的大小,必须确定帧想要的大小,确定画布的大小,然后将帧配置为具有较大的宽度和高度值。您可以使用winfo_reqwidthwinfo_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"))

相关问题 更多 >