简体   繁体   English

Python tkinter网格管理器?

[英]Python tkinter grid manager?

I just learned how to use tkinter in Python (3.2.2), and I'm having some problem using the grid manager. 我刚学会了如何在Python(3.2.2)中使用tkinter,我在使用网格管理器时遇到了一些问题。 When I put button.grid(sticky=SE), for example, the button is not being put in the bottom-right and is just being put in the upper-left, ignoring the sticky value. 例如,当我放置button.grid(sticky = SE)时,按钮没有放在右下角,只是放在左上角,忽略了粘滞值。 What am I doing wrong here? 我在这做错了什么? I tried to search it but I couldn't really find out what I am doing wrong. 我试图搜索它,但我真的不知道我做错了什么。

You probably need to set a minimum size for the widget containing the button. 您可能需要为包含按钮的窗口小部件设置最小大小。 If you don't, the container widget may shrink to occupy only the space required to display the button. 如果不这样做,容器窗口小部件可能会缩小以仅占用显示按钮所需的空间。 If so, the sticky option will be meaningless since the container widget gives no space to show any difference. 如果是这样,粘性选项将毫无意义,因为容器小部件没有空间来显示任何差异。

For example, using a tk.Frame as the container widget: 例如,使用tk.Frame作为容器小部件:

import Tkinter as tk

class SimpleApp(object):
    def __init__(self, master, **kwargs):
        title = kwargs.pop('title')
        frame = tk.Frame(master, borderwidth=5, bg = 'cyan', **kwargs)
        frame.grid()
        button = tk.Button(frame, text = title)
        button.grid(sticky = tk.SE)
        frame.rowconfigure('all', minsize = 200)
        frame.columnconfigure('all', minsize = 200)

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

yields 产量

在此输入图像描述


PS. PS。 I don't have tkinter installed in Python3.2 so I can't test this, but I think the only change you need to make this work with Python3.2 is 我没有在Python3.2中安装tkinter所以我无法对此进行测试,但我认为使用Python3.2进行此操作所需的唯一更改是

import tkinter as tk

instead of 代替

import Tkinter as tk

When you say "What am I doing wrong here", you need to post your code, otherwise how would anyone be able to guess what's wrong. 当你说“我在这里做错了什么”时,你需要发布你的代码,否则怎么会有人能猜到什么是错的。

The following works fine, placing the button in the lower right corner (SE) of the grid cell - the default is center, not upper left (NW). 以下工作正常,将按钮放在网格单元格的右下角(SE) - 默认为中心,而不是左上角(NW)。

from tkinter import Button, Label, Entry, Tk, SE
root = Tk()
Label(text="Lots o' Stuff", width=30, height=15,
      borderwidth=2, relief="raised").grid(rowspan=2)
Entry().grid(row=0, column=2)
Button(text="Hit Me").grid(row=1, column=2, sticky=SE)
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM