简体   繁体   中英

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. 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.

According to other people's answers, empty rows and columns have zero size. 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. 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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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