Tkinter多帧resiz

2024-05-15 07:47:55 发布

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

我有一个能理解多种串行协议的设备。在开发过程中,我创建了简单的tkinterui来使用这些协议。每个协议都有一个新的用户界面。由于协议有很多命令,我在一个可滚动的画布中实现了整个UI,以便在较小的显示器上使用时可以滚动。独立的UI工作得很好,我现在正试图将独立的UI组合成一个选项卡式UI。

每个UI的公共元素都是串行端口选择器,我将其分离并放入一个单独的顶部框架中。然后我实现了一个笔记本,并将每个协议UI放入每个选项卡的一个框架中。

但是我无法正确控制大小调整:我希望根窗口宽度固定在任何协议帧或串行选择器帧的最大宽度,同时禁用水平大小调整。我希望串行选择器始终存在,并且在垂直调整窗口大小时不受影响(只有笔记本被调整大小/滚动)。

下面是我到目前为止所拥有的。所有的部分都存在,但是笔记本没有填满整个窗口宽度,并且当窗口调整大小时笔记本也不会调整大小(调整大小只会添加空格)。

def main():
    ## Main window
    root = Tkinter.Tk()
    root.title("Multi-Protocol UI")

    ## Grid sizing behavior in window
    root.grid_rowconfigure(0, weight=0)
    root.grid_rowconfigure(1, weight=1)
    root.grid_columnconfigure(0, weight=1)
    root.grid_columnconfigure(1, weight=0)

    ## Window content
    upper = ttk.Frame(root)   # Serial port selector
    upper.grid(row=0)
    upper.grid_rowconfigure(0, weight=0)
    upper.grid_columnconfigure(0, weight=1)
    upper.grid_columnconfigure(1, weight=0)
    lower = ttk.Frame(root)   # For protocols
    lower.grid(row=1)
    lower.grid(row=1, sticky='nswe')
    lower.grid_rowconfigure(0, weight=1)
    lower.grid_columnconfigure(0, weight=1)
    lower.grid_columnconfigure(1, weight=0)

    # Setup serial control frame
    serial = SerialFrame(master=upper)  # Serial port selector widget + Open button

    ## Protocol GUIs are large: Use a form within a scrollable canvas.
    cnv = Tkinter.Canvas(lower)
    cnv.grid(row=0, column=0, sticky='nswe')
    cnv.grid_rowconfigure(0, weight=1)
    cnv.grid_columnconfigure(0, weight=1)
    cnv.grid_columnconfigure(1, weight=0)
    # Scrollbar for canvas
    vScroll = Tkinter.Scrollbar(
        lower, orient=Tkinter.VERTICAL, command=cnv.yview)
    vScroll.grid(row=0, column=1, sticky='ns')
    cnv.configure(yscrollcommand=vScroll.set)
    # Frame in canvas
    window = Tkinter.Frame(cnv)
    window.grid()
    # Put the frame in the canvas's scrollable zone
    cnv.create_window(0, 0, window=window, anchor='nw')

    # Setup the notebook (tabs) within the scrollable window
    notebook = ttk.Notebook(window)
    frame1 = ttk.Frame(notebook)
    frame2 = ttk.Frame(notebook)
    notebook.add(frame1, text="ProtoA")
    notebook.add(frame2, text="ProtoB")
    notebook.grid(row=0, column=0, sticky='nswe')

    # Create tab frames
    protoA = ProtoAFrame(master=frame1)
    protoA.grid()
    protoA.update_idletasks()
    protoB = ProtoBFrame(master=frame2)
    protoB.grid()
    protoB.update_idletasks()

    ## Update display to get correct dimensions
    root.update_idletasks()
    window.update_idletasks()
    ## Configure size of canvas's scrollable zone
    cnv.configure(scrollregion=(0, 0, window.winfo_width(), window.winfo_height()))

    # Not resizable in width:
    root.resizable(width=0, height=1)

    ## Go!
    root.mainloop()

如何锁定顶部串行帧,将笔记本扩展到全宽,并强制调整窗口大小以仅影响笔记本帧?我是一个新手,所以如果我错过了一些“显而易见”的东西,请温柔一点。

蒂亚!


Tags: ui协议tkinter笔记本rootwindowupperlower