Tkinter图形界面 2

fill 控件填充方式

padx - 设置水平方向的外边距

1 from tkinter import *
2 root = Tk()
3 w = Label(root, text="Red Sun", bg="red", fg="white")
4 w.pack(fill=X,padx=10)
5 w = Label(root, text="Green Grass", bg="green", fg="black")
6 w.pack(fill=X,padx=50)  #  水平外边距
7 w = Label(root, text="Blue Sky", bg="blue", fg="white")
8 w.pack(fill=X,padx=10)
9 mainloop()

pady - 设置竖直方向的外边距

1 from tkinter import *
2 root = Tk()
3 w = Label(root, text="Red Sun", bg="red", fg="white")
4 w.pack(fill=X,pady=10)
5 w = Label(root, text="Green Grass", bg="green", fg="black")
6 w.pack(fill=X,pady=10)
7 w = Label(root, text="Blue Sky", bg="blue", fg="white")
8 w.pack(fill=X,pady=10)
9 mainloop()

ipadx - 设置水平方向的内边距

1 from tkinter import *
2 root = Tk()
3 w = Label(root, text="Red Sun", bg="red", fg="white")
4 w.pack()
5 w = Label(root, text="Green Grass", bg="green", fg="black")
6 w.pack(ipadx=50)
7 w = Label(root, text="Blue Sky", bg="blue", fg="white")
8 w.pack()
9 mainloop()

ipady - 设置竖直方向的内边距

1 from tkinter import *
2 root = Tk()
3 w = Label(root, text="Red Sun", bg="red", fg="white")
4 w.pack()
5 w = Label(root, text="Green Grass", bg="green", fg="black")
6 w.pack(ipadx=10)
7 w = Label(root, text="Blue Sky", bg="blue", fg="white")
8 w.pack(ipady=10)
9 mainloop()

使用pack()函数,可以指定位置

 1 import tkinter as tk
 2 import random
 3 
 4 root = tk.Tk()
 5 # width x height + x_offset + y_offset:
 6 root.geometry("170x200+30+100")   # 设置初始窗口大小,并且初始窗口的位置
 7 root.resizable(width=False,height=False)
 8 
 9 languages = ['Python', 'Perl', 'C++', 'Java', 'Tcl/Tk']
10 labels = range(5)
11 for i in range(5):
12     ct = [random.randrange(256) for x in range(3)]
13     brightness = int(round(0.299 * ct[0] + 0.587 * ct[1] + 0.114 * ct[2]))
14     ct_hex = "%02x%02x%02x" % tuple(ct)
15     bg_colour = '#' + "".join(ct_hex)
16     l = tk.Label(root,
17                  text=languages[i],
18                  fg='White' if brightness < 120 else 'Black',
19                  bg=bg_colour)
20     l.place(x=20, y=30 + i * 30, width=120, height=25)
21 
22 root.mainloop()

 

posted @ 2020-04-19 22:15  竹石2020  阅读(159)  评论(0编辑  收藏  举报