1. 程式人生 > 實用技巧 >Python tkinter之Place

Python tkinter之Place

1、絕對和相對位置

# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *

if __name__ == '__main__':

    win = tkinter.Tk()  # 視窗
    win.title('南風丶輕語')  # 標題
    screenwidth = win.winfo_screenwidth()  # 螢幕寬度
    screenheight = win.winfo_screenheight()  # 螢幕高度
    width = 400
    height = 500
    x = int((screenwidth - width) / 2)
    y 
= int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 Label(text='標籤', relief='g').place( x=10, # 絕對的橫座標X位置 y=10, # 絕對的縱座標Y位置 anchor=NW, # 對齊方式n, ne, e, se, s, sw, w, nw, or center ) label2 = Label(text='
哈哈', relief='g') label2.place( relx=0.5, # 相對的橫座標X位置,範圍[0,1) rely=0.2, # 相對的縱座標Y位置,範圍[0,1) anchor=S, # 對齊方式 n, ne, e, se, s, sw, w, nw, or center width=200, # 元件的寬度 height=50, # 元件的高度 ) win.mainloop()