【问题标题】:Resize parts of a Python Tkinter grid调整 Python Tkinter 网格的部分大小
【发布时间】:2017-04-07 10:41:05
【问题描述】:

我只是想知道是否有一种方法可以使用 tkinter 抓取和调整网格小部件的列或行的大小(当您抓取窗口的边框并拉动它以调整窗口大小时)。

我想过一个按钮,也许是一个按下事件之类的,但我不知道如何将鼠标位置转换为列宽或行高。

我要抓取和调整大小的红色箭头]:

def initUI(self):
    self.parent["bg"] = self.main_color
    self.parent.title("Super Pipe")
    self.grid(sticky = N + S + E + W)

    self.parent.bind("<F5>", self.refresh)

    menu_bar = Menu(self.parent)

    menu_file = Menu(menu_bar, tearoff = 0)
    menu_file.add_command(label="New project", command = self.newProjectCommand)
    menu_file.add_command(label="Set project", command = self.setProjectCommand)
    menu_file.add_separator()
    menu_file.add_command(label="Quit", command = self.parent.destroy)
    menu_bar.add_cascade(label="File", menu = menu_file)

    menu_edit = Menu(menu_bar, tearoff = 0)
    menu_edit.add_command(label = "Clean backups", command = self.cleanBackupsCommand)
    menu_edit.add_command(label = "Clean student versions", command = self.cleanStudentCommand)
    menu_edit.add_separator()
    menu_edit.add_command(label = "Project settings", command = self.projectSettingsCommand)
    menu_edit.add_separator()
    menu_edit.add_command(label = "Edit custom link", command = self.editCustomLinkCommand)
    menu_edit.add_command(label = "Preferences", command = self.preferencesCommand)
    menu_bar.add_cascade(label = "Edit", menu = menu_edit)

    menu_help = Menu(menu_bar, tearoff = 0)
    menu_help.add_command(label = "Credits", command = self.about)
    menu_bar.add_cascade(label = "Help", menu = menu_help)

    self.parent.config(menu = menu_bar)

    self.parent.columnconfigure(0, pad = 0)
    self.parent.columnconfigure(1, pad = 0)
    self.parent.columnconfigure(2, pad = 0, weight = 2)
    self.parent.columnconfigure(3, pad = 0)
    self.parent.columnconfigure(4, pad = 0)

    self.parent.rowconfigure(0, pad = 0)

    ###############################################################################################################

    ## // SIDE BAR \\ ##
    left_side_bar = Frame(self.parent, bg = self.main_color)
    left_side_bar.grid(row = 0, column = 0, sticky = N)

    left_side_bar.columnconfigure(0, pad = 0)
    left_side_bar.columnconfigure(1, pad = 0)

    left_side_bar.rowconfigure(0, pad = 20)
    left_side_bar.rowconfigure(1, pad = 5)
    left_side_bar.rowconfigure(2, pad = 5)
    left_side_bar.rowconfigure(3, pad = 5)
    left_side_bar.rowconfigure(4, pad = 5)
    left_side_bar.rowconfigure(5, pad = 20)

    self.add_asset_button = Button(left_side_bar, text = "Add asset", state = DISABLED, bg = self.button_color2, activebackground = self.over_button_color2, activeforeground = self.text_color, fg = self.text_color, bd = 0, width = 8, height = 1, command = self.addAssetCommand)
    self.add_asset_button.grid(row = 0, column = 0)

    self.add_shot_button = Button(left_side_bar, text = "Add shot", state = DISABLED, bg = self.button_color2, activebackground = self.over_button_color2, activeforeground = self.text_color, fg = self.text_color, bd = 0, width = 8, height = 1, command = self.addShotCommand)
    self.add_shot_button.grid(row = 0, column = 1)

    ## ASSETS LIST ##
    asset_label = Label(left_side_bar, text = "Assets", bg = self.main_color, fg = self.text_color, font = "Helvetica 10 bold")
    asset_label.grid(row = 1, column = 0, columnspan = 2)

    self.asset_list = ttk.Treeview(left_side_bar, height = 16, show = "tree", selectmode = "browse")
    ttk.Style().configure("Treeview", background = self.list_color)
    self.asset_list.tag_configure("done", background = "#89C17F")
    self.asset_list.tag_configure("urgent", background = "#E55252")
    self.asset_list.tag_configure("high", background = "#EFB462")
    self.asset_list.tag_configure("medium", background = "#F4E255")
    self.asset_list.insert("", 1, "character", text = "CHARACTER")
    self.asset_list.insert("", 3, "fx", text = "FX")
    self.asset_list.insert("", 4, "props", text = "PROPS")
    self.asset_list.insert("", 5, "set", text = "SET")
    self.asset_list.grid(row = 2, column = 0, columnspan = 2, sticky = N + S + W + E)
    self.asset_list.bind("<ButtonRelease-1>", self.assetListCommand)

    ## SHOTS LIST ##
    shot_label = Label(left_side_bar, text = "Shots", bg = self.main_color, fg = self.text_color, font = "Helvetica 10 bold")
    shot_label.grid(row = 3, column = 0, columnspan = 2)

    self.shot_list = Listbox(left_side_bar, bg = self.list_color, selectbackground = self.second_color, bd = 0, highlightthickness = 0, width = 30, height = 31, exportselection = False)
    self.shot_list.grid(row = 4, column = 0, columnspan = 2, sticky = N + S + W + E)
    self.shot_list.bind("<<ListboxSelect>>", self.shotlistCommand)

    self.shots_preview_button = Button(left_side_bar, text = "Shots preview", state = DISABLED, bg = self.button_color2, activebackground = self.over_button_color2, activeforeground = self.text_color, fg = self.text_color, bd = 0, width = 12, height = 1, command = self.shotsPreviewCommand)
    self.shots_preview_button.grid(row = 5, column = 0, columnspan = 2)

    self.custom_button = Button(left_side_bar, text = "Custom link", bg = self.button_color2, activebackground = self.over_button_color2, activeforeground = self.text_color, fg = self.text_color, bd = 0, width = 12, height = 1, command = self.customButtonCommand)
    self.custom_button.grid(row = 6, column = 0, columnspan = 2)

    ###############################################################################################################

    separator = Frame(self.parent, bg = self.separator_color, bd = 0, width = 5, height = 10)
    separator.grid(row = 0, column = 1, sticky = N + S + W + E)

    ###############################################################################################################

    ## // SHOTS MAIN FRAME \\ ##
    self.main_area_shot = Frame(self.parent, bg = self.main_color, bd = 0)
    self.main_area_shot.grid(row = 0, column = 2, sticky = N + S + W + E)
    self.main_area_shot.pi = self.main_area_shot.grid_info()

    self.main_area_shot.columnconfigure(0, pad = 10, minsize = 40)
    self.main_area_shot.columnconfigure(1, pad = 10)
    self.main_area_shot.columnconfigure(2, pad = 10, minsize = 50)
    self.main_area_shot.columnconfigure(3, pad = 10)
    self.main_area_shot.columnconfigure(4, pad = 10)
    self.main_area_shot.columnconfigure(5, pad = 10, weight = 2)
    self.main_area_shot.columnconfigure(6, pad = 10)

    self.main_area_shot.rowconfigure(0, pad = 20, minsize = 75)
    self.main_area_shot.rowconfigure(1, pad = 5, minsize = 75)
    self.main_area_shot.rowconfigure(2, pad = 5, minsize = 410)
    self.main_area_shot.rowconfigure(3, pad = 5, minsize = 50)

【问题讨论】:

    标签: python tkinter grid resize


    【解决方案1】:

    最简单的解决方案是使用内置此功能的窗格窗口。窗格窗口用作框架,但不是使用packgrid 向其中添加小部件,而是使用@ 987654323@ 方法。小部件将在小部件之间放置一个分隔符,让您调整相对大小。窗格窗口具有水平或垂直方向。

    示例

    此示例假定 python 2.x。对于 3.x,您只需要更改导入。

    注意:tkinter 和 ttk 都提供了一个窗格窗口。以下示例使用了来自 ttk 的示例,可以说看起来更好一点。

    import Tkinter as tk
    import ttk
    
    root = tk.Tk()
    
    # the main window is divided into left and right sections,
    # and the sidebar is divided into a top and bottom section.
    pw = ttk.PanedWindow(orient="horizontal")
    sidebar = ttk.PanedWindow(pw, orient="vertical")
    main = tk.Frame(pw, width=400, height=400, background="black")
    sidebar_top = tk.Frame(sidebar, width=200, height=200, background="gray")
    sidebar_bottom = tk.Frame(sidebar, width=200, height=200, background="white")
    
    # add the paned window to the root
    pw.pack(fill="both", expand=True)
    
    # add the sidebar and main area to the main paned window
    pw.add(sidebar)
    pw.add(main)
    
    # add the top and bottom to the sidebar
    sidebar.add(sidebar_top)
    sidebar.add(sidebar_bottom)
    
    root.mainloop()
    

    【讨论】:

    • 那太好了,谢谢!我正在使用 Python 3.5.2,例如,我尝试使用 pw.paneconfigure(sidebar, minsize = 100) 将 minsize 应用于侧边栏,但它不起作用。我有一个错误的命令“paneconfigure”,你有什么想法吗?
    • @Thornydre:paneconfigure 方法适用于 tk 版本的窗格窗口。 ttk 版本有不同的选项。确保您正在阅读您正在使用的文档。
    猜你喜欢
    • 2018-12-19
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2019-06-04
    • 2011-05-18
    • 1970-01-01
    相关资源
    最近更新 更多