Python Tkinter:Paned Window不贴于顶部

时间:2017-07-24 01:23:00

标签: python tkinter

感谢您花时间看看这个。我已经挣扎了将近一个星期,它让我疯狂。

我有一个水平的Paned Window,它应该从我的工具栏的底部伸展到我的窗口的底部,但它只会粘在根窗口的底部。最后,我希望左窗格中有一个Treeview窗口小部件,右窗格中有缩略图。

任何人都可以帮助我让Paned Window坚持使用NSEW吗?我需要把它放在另一个框架内吗?

我在Windows 7上使用Python 2.7。(这不是我的整个程序,只是展示问题的示例。)

#!/usr/bin/env python
# coding=utf-8

from Tkinter import *
from ttk import *

class MainWindow:

    def null(self):
        pass

    def __init__(self):
        self.root = Tk()
        self.root.geometry("700x300")
        self.root.resizable(width=TRUE, height=TRUE)
        self.root.rowconfigure(0, weight=1)
        self.root.columnconfigure(0, weight=1)

        self.menubar = Menu(self.root)
        File_menu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="Pandoras Box", menu=File_menu)
        File_menu.add_command(label="Black Hole", command=self.null)
        self.root.config(menu=self.menubar)

        self.toolbar = Frame(self.root, relief=RAISED)
        self.toolbar.grid(row=0, column=0, sticky='NEW')
        self.toolbar.grid_columnconfigure(0, weight=1)
        self.toolbar.rowconfigure(0, weight=1)

        dummy = Button(self.toolbar, text="Tool Button")
        dummy.grid(row=0, column=0, sticky='EW')
        Find = Label(self.toolbar, text="Search")
        Search = Entry(self.toolbar)
        Find.grid(row=0, column=5, sticky='E', padx=6)
        Search.grid(row=0, column=6, sticky='E', padx=8)

        self.info_column = Frame(self.root, relief=RAISED, width=100)
        self.info_column.grid(row=0, column=5, rowspan=3, sticky='NSW')
        self.info_column.grid_rowconfigure(0, weight=1)
        self.info_column.grid_columnconfigure(0, weight=1)

        self.rootpane = PanedWindow(self.root, orient=HORIZONTAL)
        self.rootpane.grid(row=1, column=0, sticky='NS')
        self.rootpane.grid_rowconfigure(0, weight=1)
        self.rootpane.grid_columnconfigure(0, weight=1)

        self.leftpane = Frame(self.rootpane, relief=RAISED)
        self.leftpane.grid(row=0, column=0, sticky='NSEW')

        self.rightpane = Frame(self.rootpane, relief=RAISED)
        self.rightpane.grid(row=0, column=0, sticky='NSEW')

        ''' THESE BUTTONS ARE SUPPOSED TO BE INSIDE PANED WINDOW STUCK TO THE TOP!'''
        but_left = Button(self.leftpane, text="SHOULD BE IN LEFT PANE UNDER TOOLBAR FRAME")
        but_left.grid(row=0, column=0, sticky='NEW')

        but_right = Button(self.rightpane, text="SHOULD BE IN RIGHT PANE UNDER TOOLBAR FRAME")
        but_right.grid(row=0, column=0, sticky='NEW')

        self.rootpane.add(self.leftpane)
        self.rootpane.add(self.rightpane)

        self.SbarMesg = StringVar()
        self.label = Label(self.root, textvariable=self.SbarMesg, font=('arial', 8, 'normal'))
        self.SbarMesg.set('Status Bar:')
        self.label.grid(row=3, column=0, columnspan=6, sticky='SEW')
        self.label.grid_rowconfigure(0, weight=1)
        self.label.grid_columnconfigure(0, weight=1)

        self.root.mainloop()


a = MainWindow()

1 个答案:

答案 0 :(得分:2)

简短回答:您在按钮和工具栏框架之间看到的空间是因为您允许包含工具栏的行调整大小,而不是包含PanedWindow的行...为了得到你的内容想要,替换:

self.root.rowconfigure(0, weight=1)

self.root.rowconfigure(1, weight=1)

其他评论

  • 尝试avoid wildcard imports。在这种情况下,很难区分tkttk小部件
  • 要允许调整使用grid()对齐的小部件的大小,必须在小部件的父级而不是小部件本身上调用.rowconfigure(..., weight=x)
  • 背景颜色对调试tkinter中的对齐问题非常有用。

<强>代码:

import Tkinter as tk
import ttk

class MainWindow:

    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("700x300")
        self.root.resizable(width=tk.TRUE, height=tk.TRUE)
        self.root.rowconfigure(1, weight=1)
        self.root.columnconfigure(0, weight=1)

        self.toolbar = tk.Frame(self.root, relief=tk.RAISED, bg="yellow")
        self.toolbar.grid(row=0, column=0, sticky='NEW')
        self.toolbar.columnconfigure(0, weight=1)

        dummy = ttk.Button(self.toolbar, text="Tool Button")
        dummy.grid(row=0, column=0, sticky='EW')
        Find = tk.Label(self.toolbar, text="Search")
        Search = ttk.Entry(self.toolbar)
        Find.grid(row=0, column=5, sticky='E', padx=6)
        Search.grid(row=0, column=6, sticky='E', padx=8)

        self.info_column = tk.Frame(self.root, relief=tk.RAISED, width=100, bg="orange")
        self.info_column.grid(row=0, column=5, rowspan=2, sticky='NSW')

        self.rootpane = tk.PanedWindow(self.root, orient=tk.HORIZONTAL, bg="blue")
        self.rootpane.grid(row=1, column=0, sticky='NSEW')

        self.leftpane = tk.Frame(self.rootpane, bg="pink")
        self.rootpane.add(self.leftpane)

        self.rightpane = tk.Frame(self.rootpane, bg="red")
        self.rootpane.add(self.rightpane)

        ''' THESE BUTTONS ARE SUPPOSED TO BE INSIDE PANED WINDOW STUCK TO THE TOP!'''
        but_left = ttk.Button(self.leftpane, text="SHOULD BE IN LEFT PANE UNDER TOOLBAR FRAME")
        but_left.grid(row=0, column=0, sticky='NEW')

        but_right = ttk.Button(self.rightpane, text="SHOULD BE IN RIGHT PANE UNDER TOOLBAR FRAME")
        but_right.grid(row=0, column=0, sticky='NEW')

        self.label = tk.Label(self.root, text="Status:",  anchor="w")
        self.label.grid(row=3, column=0, columnspan=6, sticky='SEW')

        self.root.mainloop()

a = MainWindow()