Python Tkinter/Pmw——笔记本框架内的定心框架

Python Tkinter/Pmw——笔记本框架内的定心框架,python,tkinter,Python,Tkinter,我想做的是把两个按钮(sframe)放在笔记本(master)框架的中间。这在Python2.4上没有问题,但在Python2.7上,默认情况下,框架锚定到NW。我知道如果我使用rowconfigure()/columnconfigure()母版页框架,内部框架将居中,但此解决方案似乎不正确。禁用传播和更改行/列权重似乎也没有帮助。有没有办法让内部框架正确居中?以下是我正在使用的测试代码: import Tkinter as tk, Tkinter import Pmw class Simpl

我想做的是把两个按钮(sframe)放在笔记本(master)框架的中间。这在Python2.4上没有问题,但在Python2.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()

请告诉我是否可以提供任何其他信息。

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

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

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

啊。这是有道理的。谢谢你的解释!