• 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()
  • 相关阅读:
    .net开发微信公众号(2)-验证服务器地址
    [广度遍历和深度遍历]聊聊算法和设计模式
    DDD领域驱动设计之聚合、实体、值对象
    DDD领域驱动设计之运用层代码
    DDD领域驱动设计之领域服务
    DDD领域驱动设计之领域基础设施层
    DDD领域驱动设计实践篇之如何提取模型
    Docker最全教程——从理论到实战(七)
    Docker最全教程——从理论到实战(六)
    Docker最全教程——从理论到实战(五)
  • 原文地址:https://www.cnblogs.com/ch2020/p/12734593.html
Copyright © 2020-2023  润新知