zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    探究操作系统的内存分配(malloc)对齐策略
    三十一个实用的小常识
    防止网页后退
    郁闷的一天
    脑袋不行
    家的开张
    猴子定律
    赴微软onsite!谁有C++/HTML/JavaScript开发工程师推荐?
    卡马克的求平方根函数代码的陷阱
    动作游戏自定义技能探讨
  • 原文地址:https://www.cnblogs.com/ch2020/p/12734593.html
Copyright © 2011-2022 走看看