Tkinter格式化每个oth旁边的多个帧

2024-03-28 21:44:53 发布

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

因此,我正在创建一个GUI,实际上我希望在GUI的顶部有两个不同的“工具栏”,类似于this
目前,我把每个工具栏的按钮放在两个不同的框架中,分别称为toolbar和Selectbar。在每个按钮上我调用.pack()在工具栏中格式化它们,然后在每个工具栏上调用
toolbar.grid(row=0, column=0, sticky='NW')
selectbar.grid(row=0, column=1, sticky='NE')
但是,我不相信这是对的,因为它们是两个不同的“网格”,它试图将它们放在各自的列中。它仍然给了我一些接近于这个目标的产品:this
但是,我想知道如何将这两个框架“组合”成一个更大的各自的框架,这样我就可以使用.configurecolumn(0, weight=1)使第一个工具栏在屏幕上延伸得更远。在

本质上,我想知道我怎么能把这两个“工具栏”放在一起,但第一个是用空白来扩展的。在

编辑:这里是代码,有些地方省略了。在

  from tkinter import *  
  from MenuBar import *  
  from ToolBar import *  
  import tkinter.ttk

class App(Tk):
def __init__(self):
    Tk.__init__(self)


    #Creates the MenuBar
    menubar = MenuBar(self)
    self.config(menu=menubar)

    #Creates the ToolBar
    toolbar = Frame(bg="#f2f2f2", bd=1, relief=RAISED, width=1000)

    newUndIcon = itk.PhotoImage(file="Icons/newUndirected.png")
    newDirIcon = itk.PhotoImage(file="Icons/newDirected.png")
    b0 = Button(toolbar, text="Create a new undirected graph", image=newUndIcon, relief=FLAT)
    b1 = Button(toolbar, text="Create a new directed graph", image=newDirIcon, relief=FLAT)
    b2 = Button(toolbar, text="Open an existing graph", image=openIcon, relief=FLAT)
    b3 = Button(toolbar, text="Save graph", image=saveIcon, relief=FLAT)
    b0.img = newUndIcon
    b1.img = newDirIcon
    b2.img = openIcon
    b3.img = saveIcon
    b0.pack(side=LEFT, padx=2, pady=2)
    b1.pack(side=LEFT, padx=2, pady=2)
    b2.pack(side=LEFT, padx=2, pady=2)
    b3.pack(side=LEFT, padx=2, pady=2)       

    #toolbar.pack(side=TOP, fill=X)
    toolbar.grid(row=0, sticky='NW')
    toolbar.columnconfigure(1, weight=1)

    selectBar = Frame(bg="#f2f2f2", bd=1, relief=FLAT)
    c0 = Button(selectBar, image=newUndIcon, relief=FLAT)
    c1 = Button(selectBar, image=newDirIcon, relief=FLAT)
    c2 = Button(selectBar, image=vertexIcon, relief=FLAT)

    c0.img = newUndIcon
    c1.img = newDirIcon
    c2.img = vertexIcon

    c0.pack(side=LEFT, padx=2, pady=2)
    c1.pack(side=LEFT, padx=2, pady=2)
    c2.pack(side=LEFT, padx=2, pady=2)

    selectBar.grid(row=0, column=1, sticky='NW')
    selectBar.columnconfigure(1, weight=1)



  app=App()
  app.iconbitmap('Icons/titleIcon.ico')
  app.title("GMPX")
  app.geometry('900x600')
  app.config(bg="#FFF")
  app.mainloop()
  app.destroy()

Tags: imageappimgbuttonleftsidepack工具栏
1条回答
网友
1楼 · 发布于 2024-03-28 21:44:53

您可以尝试将toolbarselectBar放在Frame内,并使用pack(),而不是{}:

topbar = Frame(self)
....
toolbar = Frame(topbar, ...)
toolbar.pack(side=LEFT, fill=X, expand=True)
...
selectBar = Frame(topbar, ...)
selectBar.pack(side=RIGHT)
...
topbar.pack(fill=X)

相关问题 更多 >