亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在窗口中居中許多小部件 [tkinter]

在窗口中居中許多小部件 [tkinter]

千萬里不及你 2023-06-13 17:15:44
我需要將兩個按鈕居中,我可能需要將更多按鈕居中,但我不能居中超過一個按鈕,所以我需要幫助......這是代碼:from tkinter import *from tkinter.ttk import * import osroot = Tk()root.geometry("325x100")    def click():    pass        def click2():    pass            button = Button(root, text="Button 1", command=click, width=25)button.grid(row=0, column=0)button2 = Button(root, text="Button 2", command=click2, width=25)button2.grid(row=1, column=0)      root.grid_rowconfigure(0, weight=1)root.grid_columnconfigure(0, weight=1)      root.mainloop()
查看完整描述

2 回答

?
莫回無

TA貢獻1865條經驗 獲得超7個贊

我做了一些測試,這就是我想出的。我使用了 .pack() 方法而不是 .grid() 方法,我還使用了一個框架。我是 Python 的新手,但在這里 :)


from tkinter import *

from tkinter.ttk import *

import os


root = Tk()

root.geometry("325x100")


def click():

    pass


def click2():

    pass


frame = Frame(root)

frame.pack(padx = 20, pady = 12)


button = Button(root, text="Button 1", command=click, width=25)

button.pack()


button2 = Button(root, text="Button 2", command=click2, width=25)

button2.pack()



root.mainloop()

這是它的樣子:

http://img1.sycdn.imooc.com//648834120001219303490137.jpg

查看完整回答
反對 回復 2023-06-13
?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

不要在第一行增加重量。它迫使它擴張。不過,您可能還想考慮其他事情。您最終可能會在該行上放置其他東西,并且您可能需要那個東西來擴展該行。在當前狀態下,這將導致您遇到“第 22 條軍規”。您可能需要考慮創建一個框架來容納所有按鈕,并將整個框架放在根部。


立即修復:


from tkinter import *

from tkinter.ttk import * 

import os


root = Tk()

root.geometry("325x100")

    

def click():

    pass

        

def click2():

    pass

            

button = Button(root, text="Button 1", command=click, width=25)

button.grid(row=0, column=0)


button2 = Button(root, text="Button 2", command=click2, width=25)

button2.grid(row=1, column=0)


#this is forcing the top row to expand     

#root.grid_rowconfigure(0, weight=1)   

root.grid_columnconfigure(0, weight=1)

      

root.mainloop() 

可能是更好的方法:


from tkinter import *

from tkinter.ttk import * 

import os


root = Tk()

root.geometry("325x100")

    

def click():

    pass

        

def click2():

    pass

    

#by not defining row and column in grid() 

#~ row will be the next available one and column will be 0

    

button_frame = Frame(root)

button_frame.grid(sticky='nswe')

button_frame.grid_columnconfigure(0, weight=1)


#you only need to store a reference if you intend to change/reference/destroy/forget these

#if they are going to always be a button, as initially defined, a reference is dead weight          

Button(button_frame, text="Button 1", command=click, width=25).grid()

Button(button_frame, text="Button 2", command=click2, width=25).grid()


#now you can use grid_rowconfigure without it destroying your button layout      

root.grid_rowconfigure(0, weight=1)

root.grid_columnconfigure(0, weight=1)

      

root.mainloop() 


查看完整回答
反對 回復 2023-06-13
  • 2 回答
  • 0 關注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號