简体   繁体   English

无法使用网格移动对象

[英]Can't move object with grid

I've researched this issue, and found people with similar problem. 我研究了这个问题,并发现了类似问题的人。 However, the answers are not clear for me. 但是,答案对我来说还不清楚。

I'm trying to freely move my objects into X rows and columns. 我试图将我的对象自由地移动到X行和列中。 Right now, regardless of what value I put into column or row my object won't change position, it's always on the top-left corner. 现在,无论我在对象的columnrow中输入什么值,我的对象都不会改变位置,它始终位于左上角。

According to other people's answers, empty rows and columns have zero size. 根据其他人的答案,空的rowscolumns大小为零。 How can I change that? 我该如何改变?

This is my current code: 这是我当前的代码:

from tkinter import *

class Application:
    def __init__(self, master):
        frame = Frame(master)

        b = Button(text="This is a button")
        b.grid(row=2, column=3)

root = Tk()
root.geometry('{}x{}'.format(500, 300))
app = Application(root)
root.mainloop()

Yes, empty rows and columns have zero size. 是的,空行和列的大小为零。 So, without the help of any other options, your widget will be placed at the corner of the window. 因此,无需任何其他选项的帮助,您的小部件将放置在窗口的角落。 But with the help of options like padx , pady , sticky and functions like grid_columnconfigure() and grid_rowconfigure() , you can achieve what you want. 但是借助padxpadysticky等选项以及grid_columnconfigure()grid_rowconfigure() ,您可以实现所需的功能。 As an example, look at this: 例如,看一下这个:

from tkinter import *

class Application:
    def __init__(self, master):
        frame = Frame(master)
        b = Button(text="This is a button")
        b.grid(row=0, column=0, pady=(20,0))
        master.grid_columnconfigure(0, weight=1)

root = Tk()
root.geometry('{}x{}'.format(500, 300))
app = Application(root)
root.mainloop()

在此处输入图片说明

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

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