Tkinter / Pmw - 笔记本框架内的中心框架

时间:2014-04-08 19:34:34

标签: python tkinter

我尝试做的是在笔记本(主)框架内居中两个按钮(sframe)的框架。这在Python 2.4上没有问题,但在Python 2.7上,框架默认锚定到NW。我知道如果我将rowconfigure()/ columnconfigure()作为主页框架,内框架将自己居中,但这个解决方案看起来并不正确。禁用传播和更改行/列权重似乎也无济于事。无论如何只是让内框正确居中?以下是我正在使用的测试代码:

import Tkinter as tk, Tkinter
import Pmw

class SimpleApp(object):
  def __init__(self, master, **kwargs):
    title = kwargs.pop('title')
    master.configure(bg='blue')
    sframe = tk.Frame(master, relief=tk.RIDGE, bd=5, width=100,bg='green')
    sframe.grid()
    button = tk.Button(sframe, text = title)
    button.grid(sticky = tk.W)
    button = tk.Button(sframe, text = 'next')
    button.grid(sticky = tk.E)
    #sframe.propagate(0)
    #master.rowconfigure(0, minsize = 300)
    #master.columnconfigure(0, minsize = 300)

class Demo:
  def __init__(self, parent):
    # Create and pack the NoteBook.
    notebook = Pmw.NoteBook(parent)
    notebook.pack(fill = 'both', expand = 1, padx = 10, pady = 10)

    # Add the "Appearance" page to the notebook.
    page = notebook.add('Helpers')
    app = SimpleApp(page, title= 'hello, world')
    notebook.tab('Helpers').focus_set()
    page = notebook.add('Appearance')

    # Create the "Toolbar" contents of the page.
    group = Pmw.Group(page, tag_text = 'Toolbar')
    group.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
    b1 = Tkinter.Checkbutton(group.interior(), text = 'Show toolbar')
    b1.grid(row = 0, column = 0)
    b2 = Tkinter.Checkbutton(group.interior(), text = 'Toolbar tips')
    b2.grid(row = 0, column = 1)

    # Create the "Startup" contents of the page.
    group = Pmw.Group(page, tag_text = 'Startup')
    group.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
    home = Pmw.EntryField(group.interior(), labelpos = 'w',
        label_text = 'Home page location:')
    home.pack(fill = 'x', padx = 20, pady = 10)

    page = notebook.add('Images')

    notebook.setnaturalsize()

def basic():
  root = tk.Tk()
  #app = SimpleApp(root, title = 'Hello, world')
  app = Demo(root)
  root.mainloop()
basic()

如果我能提供任何其他信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

您需要在master中配置第0行和第0列的权重:

master.grid_columnconfigure(0, weight=1)
master.grid_rowconfigure(0, weight=1)

您将内部sframe放置在master的第0行第0列中,由于该单元格没有重量,因此会缩小到左上角。赋予行和列权重1使得列和行填充可用空间。由于您没有为sframe使用任何粘性选项,因此它将保持在其单元格的中心而不是填充其单元格。