python tkinter place layout

The place layout allows you to specify the position (x, y) and size (width, height) of the control through the parameters of the place method
. Label().place(x=10,y=10,width=100,height=20) The
first one is fixed background color

Insert picture description here

# place布局
from tkinter import *
import random
window = Tk()#创建Tk类实例(也就是要显示的窗口)
window.title("dalaojun")#窗口标题
window['background']='Yellow'#窗口的背景颜色
window.geometry("200x200+30+30")#宽200高200 距离屏幕左上角横纵为30,30像素

Supper=["羊肉串","牛肉","鸡翅","鸡腿","猪鞭"]#列表数据
for i in range(5):
    Label(window,text=Supper[i],fg="White",bg='pink').place(x =25,y =30+i*30,width=120,height=25)
mainloop()#调用mainloop函数进入事件循环
# 因为是from tkinter import * 所以仅仅为mainloop()
# 如果是import tkinter 则为tkinter.mainloop()

Add fill random color
Insert picture description here

# place布局
from tkinter import *
import random
window = Tk()#创建Tk类实例(也就是要显示的窗口)
window.title("dalaojun")#窗口标题
window['background']='Yellow'#窗口的背景颜色
window.geometry("200x200+30+30")#宽200高200 距离屏幕左上角横纵为30,30像素


Supper=["羊肉串","牛肉","鸡翅","鸡腿","猪鞭"]
labels = range(5)

for i in range(5):
    # 产生3个随机值,范围在0-255 赋值于变量ct列表中,如列表:[127, 245, 71]
    ct =[random.randrange(256) for x in range(3)]
    # RGB转换成灰度图像的一个常用公式是:Gray = R*0.299 + G*0.587 + B*0.114
    # round() 方法返回浮点数x的四舍五入值 如 round(80.23456, 2) ==80.23
    # int() 函数用于将一个字符串或数字转换为整型int(3.6)==3
    # 下面为取亮度
    brightness = int(round(0.299*ct[0] + 0.587*ct[1] + 0.144*ct[2]))#得到一个数值如:192
    # 把ct列表转换为元组 tuple(ct) 得到一个元组如:(127, 245, 71)
    # 并且把该元组转换为十六进制形式 赋值变量ct_hex 得到的结果如:7ff547
    ct_hex ="%02x%02x%02x" % tuple(ct)
    # 颜色使用字符串添加方法join把#号与7ff547组合为#7ff547
    bg_colour = "#" +"".join(ct_hex)
    # 当背景亮度小与120时为白色字体 大与120亮度则为黑色
    label = Label(window,text=Supper[i],fg="White" if brightness <120 else "Black",bg=bg_colour)
    # x=25 是该控件距离窗口的左边宽度
    # y =30+i*30 是该控件距离窗口上方的高度
    #width 和height分别为该控件的自身宽高
    label.place(x =25,y =30+i*30,width=120,height=25)
'''
数据测试
tt =[random.randrange(256) for x in range(3)]
print(tt)
brightnesss = int(round(0.299*tt[0] + 0.587*tt[1] + 0.144*tt[2]))
print(brightnesss)
kk=tuple(tt)
print(kk)
pp ="%02x%02x%02x" % tuple(kk)
print(pp)
'''
mainloop()

Guess you like

Origin blog.csdn.net/weixin_47021806/article/details/115192287