python tkinter中的錨點(anchor)問題及處理
tkinter的錨點(anchor)問題
tkinter中anchor參數
(注意,參數的英文都是小寫)
| 字母 | 方位 |
|---|---|
| n | 北 |
| s | 南 |
| w | 西 |
| e | 東 |
| center | 中心 |
| nw | 西北 |
| ne | 東北 |
| sw | 西南 |
| se | 東南 |
from tkinter import *
from tkinter import messagebox as box
def main_menu():
window = Tk()
window.title('Juke Box')
window.geometry('800x480')
window.configure(background = 'black')
label = Label(window, text = 'Juke-Box', fg = 'light green', bg = 'black', font = (None, 30), height = 2)
label.pack(side = TOP)
Jam = Button(window, text = 'The Jam', width = 25, height = 2)
Jam.pack(pady = 10, padx = 25, anchor = 'n')
Roses = Button(window, text = 'The Stone Roses', width = 25, height = 2)
Roses.pack(pady = 10, padx = 25, anchor = 'w')
Smiths = Button(window, text = 'The Smiths', width = 25, height = 2)
Smiths.pack(pady = 10, padx = 25, anchor = 'w')
Wedding = Button(window, text = 'The Wedding Pressent', width = 25, height = 2)
Wedding.pack(pady = 10, padx = 25, anchor = 'w')
Blondie = Button(window, text = 'Blondie', width = 25, height = 2)
Blondie.pack(pady = 10, padx = 25, anchor = 'w')
Clash = Button(window, text = 'Clash', width = 25, height = 2)
Clash.pack(pady = 10, padx = 25, anchor = 'w')
Madness = Button(window, text = 'Madness', width = 25, height = 2)
Madness.pack(pady = 10, padx = 25, anchor = 'n')
Pistols = Button(window, text = 'The Sex Pistols', width = 25, height = 2)
Pistols.pack(pady = 10, padx = 25, anchor = 'n')
window.mainloop()
main_menu()幾何管理方法place中anchor的含義
關于place的用法,重點在于理解anchor的用法。
在一個大矩形中準確地定義一個小矩形(有面積,不是點)的位置,需要知道三個信息:一是坐標系定義,二是坐標數據,三是要在小矩形上指定一個定位點。原點和坐標系完全是系統(tǒng)默認的定義方式,即原點在master控件的左上角,向右向下為正。
坐標數據以相對形式給出,取0到1之間的浮點數。如果取0,則參考點橫坐標為0(在最左側),如果取1,則參考點橫坐標為master控件的最右側,縱坐標也是如此。
定位點anchor的定義,這個是最終定位的關鍵。如圖:

如圖,定義位置時,以master控件的左上角為原點,以給定的相對坐標(這里都是0.5)指定位置。
每個控件都有9個anchor,選一個作為“把手”,將這個“把手”安放在指定位置即可。
anchor='nw’的含義是“將控件的左上角安放在指定位置”。
再如:

anchor='n’的含義是“將控件的上邊中點安放在指定位置”,其他的anchor同理。這可能就是為什么很多python技術文檔中,習慣先指定坐標,最后再選擇anchor。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python Matplotlib數據可視化(2):詳解三大容器對象與常用設置
這篇文章主要介紹了python Matplotlib三大容器對象與常用設置的相關資料,幫助大家更好的學習和使用Matplotlib庫的用法,感興趣的朋友可以了解下2020-09-09
抵御代碼復雜性使python函數更加Pythonic技巧示例詳解
這篇文章主要介紹了抵御代碼復雜性使python函數更加Pythonic技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
python實現linux服務器批量修改密碼并生成execl
這篇文章主要介紹了python實現linux服務器批量修改密碼并生成execl示例,需要的朋友可以參考下2014-04-04
Python中線程threading.Thread的使用詳解
python的thread模塊是比較底層的模塊,python的threading模塊是對thread做了一些包裝的,可以更加方便的被使用。本文將為大家詳細介紹一下python中的線程threading.Thread()的使用,需要的可以參考一下2022-07-07

