【发布时间】:2012-11-07 15:26:58
【问题描述】:
我有一台可以理解多种串行协议的设备。在开发过程中,我创建了简单的 Tkinter UI 来使用协议。每个协议都有一个新的 UI。由于协议有很多命令,我在一个可滚动的画布中实现了整个 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()
如何锁定顶部串行框架,将笔记本电脑扩展至全宽,并强制调整窗口大小以仅影响笔记本电脑框架?我是 Tkinter 新手,所以如果我遗漏了一些“明显”的东西,请保持温柔。
TIA!
【问题讨论】:
-
部分进度:在根窗口中,我需要将顶部框架的行配置权重设置为 0,将底部框架设置为 1。上面的源代码已更新。但是笔记本仍然没有扩展以填充下框架。咕噜。
-
我已将 rowconfigure 和 columnconfigure 添加到所有帧,并实现了许多所需的行为。但默认窗口宽度是相当窄的顶部框架的宽度,而不是最宽的笔记本选项卡的宽度。
-
我目前对宽度问题的处理是填充顶部窗口。不漂亮,但效果很好。
标签: python resize tkinter frame scrollable