[英]How can i make a tkinter Label not ajust the column size using Grid?
我需要在程序窗口的頂部添加一個包含標題的黑條,例如,請查看此網頁的頂部-有一個標題,這基本上就是我想要給tkinter的每個窗口使用的標題程序。 無論如何,我可以為我創建的每個新窗口設置默認值嗎? 還是我可以創建一種默認格式,該格式將自動分配給每個窗口/頁面?
我嘗試使用標簽在屏幕上延伸,如下所示:
label1 = tkinter.Label(window, text="Title ",bg="black",fg="white")
label1.grid(row=0,column=0)
我還嘗試了其他方法,但是問題在於它調整了網格的列大小,這意味着我只能有一列小部件。
僅供參考,我14歲,仍在學習基礎知識,因此我使用一種簡單的使用tkinter的方式。
這是我如何基本使用tkinter的一個示例,所以請耐心等待我哈哈。
#How i create main window.
window1 = tkinter.Tk()
#Insert window1 configurations(e.g, title, background, icon)
#How i create new windows
window2 = tkinter.TopLevel()
#Insert window2 configurations(e.g, title, background, icon)
window2.withdraw()
#How i open a new window
def openwindow():
window1.withdraw
window2.update
window2.deiconify
現在我的問題在此代碼內:
#New page
def SongPage():
MainMenu.withdraw()
#Newpage
def OptionsPage():
MainMenu.withdraw()
#MainMenu window.
MainMenu = tkinter.Tk()
MainMenu.geometry("200x150")
MainMenu.configure(background="grey")
MainMenu.title("Title")
#This is the header.(It has spaces to make it spread across screen)
MenuTitle = tkinter.Label(MainMenu, text="Header ",bg="black",
fg="white",font=("Times",20,"bold"))
#This is a small subheader.(It has spaces to make it spread across screen)
MenuSub = tkinter.Label(MainMenu, text=" Subheading
",bg="grey34", fg="white",font=("Times",10,"bold"))
#These "space" labels are to just space things out.
space3 = tkinter.Label(MainMenu, text="",bg="grey", fg="white",
font=("Times",20,"bold"))
space4 = tkinter.Label(MainMenu, text="",bg="grey", fg="white",
font=("Times",20,"bold"))
#These are the two buttons i'd like to be on screen under headers.
SongButton = tkinter.Button(MainMenu, text="Songs", bg="grey",fg="black",
font=("Times",12),command=SongPage)
OptionsButton = tkinter.Button(MainMenu, text="Options", bg="grey",fg="black",
font=("Times",12),command=OptionsPage)
#This is applying the widgets to the window.
MenuTitle.grid(row=0,column=0)
MenuSub.grid(row=1,column=0)
SongButton.grid(row=3,column=0)
OptionsButton.grid(row=3,column=1)
這是當前外觀的屏幕截圖: https : //pasteboard.co/HZKnIR9.png
這是我想要的樣子: https : //pasteboard.co/HZKo1Ru.png
您可以在對.grid()的調用中使用columnspan關鍵字參數。 然后,您可以使用sticky關鍵字來確保它觸及列的左右邊緣。 例如:
label1.grid(row=0, column=0, columnspan=2, sticky='ew')
將導致標簽在第0列和第1列上延伸。如果窗口只有兩列,則黑條將始終延伸到整個列。 如果您有兩列以上,則需要更大的列數。
Tkinter將首先通過使用其他小部件的大小來計算所有列的寬度。 如果黑條太大,需要更多空間,則TkInter將不得不拉伸其他列寬。 但這可能不是必需的,黑條將一直很好地貫穿整個窗口,這就是您想要的。
請參閱此處的文檔: http : //infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.