框架未展开

时间:2018-10-03 20:08:07

标签: python tkinter

也许这就是Tkinter的工作方式,但我不确定。目前,我有一个主窗口,其中三个框架彼此相邻布置。框架ContainerFrame是一个主框架,然后将characterFrame和planetFrame放置在ContainerFrame内。问题是或我想发生的事情是,框架将填充窗口的设置区域,而不管框架中是否是数据/小部件。 这是我设想的样子,https://imgur.com/OjdKFh4

from tkinter import *
from tkinter import ttk

MainWindow = Tk()
def mainWindow():
  MainWindow.option_add('*tearOff', False)
  MainWindow.title("Interface")
  MainWindow.geometry('800x600')
  menubar = Menu(MainWindow)
  MainWindow.config(menu = menubar)
  File = Menu(menubar)
  About = Menu(menubar)
  menubar.add_cascade(menu = File, label = "File")
  menubar.add_cascade(menu = About, label = "About")

def frameContainer():
  containerFrame = Frame(MainWindow)
  containerFrame.pack(anchor = "nw", side = "left", fill = "both", expand = False)
  scroller = Scrollbar(orient = "vertical")

  characterFrame = Frame(containerFrame, borderwidth="2", relief="sunken")
  characterFrame.pack(anchor = "nw", side = "left", expand = True)

  planetFrame =  Frame(containerFrame ,borderwidth="2", relief="sunken")
  planetFrame.pack(anchor = "nw", side = "right", expand = True)

  scroller = Scrollbar(orient = "vertical")
  scroller.pack(anchor = "e", side = "right", fill = "y", expand = False)

  characterLable = Button(characterFrame, text ="Characters")
  characterLable.pack()
  Label(characterFrame, text ="Test1").pack()
  Label(characterFrame, text ="Test2").pack()

  Label(planetFrame, text ="Test1").pack()
  Label(planetFrame, text ="Test2").pack()


mainWindow()
frameContainer()
MainWindow.mainloop()

1 个答案:

答案 0 :(得分:1)

通常,您将使用框架来组织带有滚动条的小部件,但是框架不可滚动。如果要滚动包含其他小部件的区域,通常要做的是使用画布。

研究本指南:Tkinter Scrollbar Patterns

Pack可能很难使用,而我发现克服此问题的唯一方法是继续尝试。如果让不同的帧具有不同的bg颜色,通常更容易查看正在执行的操作。另外,我还自由更改了一些变量名,因为它们没有暗示它们是什么,或者与其他名称太相似,例如。 mainWindowMainWindow

我在一些小部件中添加了一些填充,以使其看起来更好。

from tkinter import *

root = Tk()

def create_main_window():
    root.option_add('*tearOff', False)
    root.title("Interface")
    root.geometry('400x300+800+50')

    menubar = Menu(root)
    root.config(menu = menubar)
    File = Menu(menubar)
    About = Menu(menubar)
    menubar.add_cascade(menu = File, label = "File")
    menubar.add_cascade(menu = About, label = "About")

def create_container_frame():
    container = Frame(root, bg='tan')
    container.pack(fill="both", expand=True)

    scroller = Scrollbar(container, orient="vertical")
    scroller.pack(side="right", fill="y")

    characterFrame = Frame(container, bd=2, relief="sunken", bg='thistle')
    characterFrame.pack(side="left", fill='y', padx=(10,0), pady=10)

    character_button = Button(characterFrame, text ="Characters")
    character_button.pack(padx=10, pady=(10,0))
    Label(characterFrame, text ="Test1").pack()
    Label(characterFrame, text ="Test2").pack()

    planetFrame = Frame(container ,bd=2, relief="sunken", bg='khaki')
    planetFrame.pack(side="left", fill='both', expand=True, padx=10, pady=10)

    Label(planetFrame, text="Test1").pack(pady=(10,0))
    Label(planetFrame, text="Test2").pack()

create_main_window()
create_container_frame()

root.mainloop()

这是您想要的布局吗?