python使用tkinter調(diào)整label背景顏色的測試
更新時間:2023年09月06日 09:32:48 作者:qq_278667286
這篇文章主要介紹了python使用tkinter調(diào)整label背景顏色的測試方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
調(diào)整label背景顏色測試截圖

我們都知道,一般來說每個像素的數(shù)據(jù)包括rgb三個通道用三個字節(jié)來表示
如0xff9900 中 ff是紅色的量 99是綠色的量 00是藍色的量
代碼實現(xiàn)了用 scale滑動條來調(diào)節(jié) rgb的占比,從而調(diào)節(jié)顏色
源碼
import tkinter as tk
window = tk.Tk() # 實例化一個窗口
window.title('Color setting') # 定義窗口標(biāo)題
window.geometry('400x600') # 定義窗口大小
l = tk.Label(window, bg='yellow', width=200, height=2, text='empty')
l.pack()
r,g,b,w=100,100,100,100
def print_selection():
global r,g,b,w
#轉(zhuǎn)化16進制并格式化
rv =hex(int(255.0*(float(r)*float(w)/10000.0)))#0x xx 不合適
srv='%02x'%int(255.0*(float(r)*float(w)/10000.0))
sgv = '%02x' % int(255.0 * (float(g) * float(w) / 10000.0))
sbv = '%02x' % int(255.0 * (float(b) * float(w) / 10000.0))
#print(srv)
bgstr="#"+srv+sgv+sbv
l.configure(bg=bgstr)
l.config(text='R:' + str(r) + '%,G:' + str(g) + '%,B:' + str(b) + '%,W:' + str(w)+"% C:"+bgstr)
jsonstr="\"color\":{\"r\":%1.2f,\"g\":%1.2f,\"b\":%1.2f,\"w\":%1.2f}"%(float(r)/100.0,float(g)/100.0,float(b)/100.0,float(w)/100.0)
fresh(jsonstr)
def setR(v):
global r
r=v
print_selection()
def setG(v):
global g
g=v
print_selection()
def setB(v):
global b
b=v
print_selection()
def setW(v):
global w
w=v
print_selection()
rs = tk.Scale(window, label='R:', from_=0, to=100, orient=tk.HORIZONTAL, length=200, showvalue=1, tickinterval=25,
resolution=1, command=setR)
rs.pack() # 顯示名字 從5-11 條方向 長度(像素),是否直接顯示值,標(biāo)簽的單位長度,保留精度 ,定義功能
rs.set(r)
gs = tk.Scale(window, label='G:', from_=0, to=100, orient=tk.HORIZONTAL, length=200, showvalue=1, tickinterval=25,
resolution=1, command=setG)
gs.pack()
gs.set(g)
bs = tk.Scale(window, label='B:', from_=0, to=100, orient=tk.HORIZONTAL, length=200, showvalue=1, tickinterval=25,
resolution=1, command=setB)
bs.pack()
bs.set(b)
ws = tk.Scale(window, label='W:', from_=0, to=100, orient=tk.HORIZONTAL, length=200, showvalue=1, tickinterval=25,
resolution=1, command=setW)
ws.pack()
ws.set(w)
text1 = tk.Text(window,width=30,height=5)
text1.pack()
text1.insert(tk.INSERT,'I love you')
def fresh(t):
text1.delete(1.0, tk.END)
text1.insert(tk.INSERT, t)
def show():
T1 = text1.get(0.0, tk.END)
print(T1)
def cut(event=None):
text1.event_generate("<<Cut>>")
def copy(event=None):
text1.get('sel.first', 'sel.last')
text1.event_generate("<<Copy>>")
def paste(event=None):
text1.event_generate('<<Paste>>')
# Select all the text in textbox
def select_all(event=None):
text1.tag_add(tk.SEL, "1.0", tk.END)
text1.mark_set(tk.INSERT, "1.0")
text1.see(tk.INSERT)
copy()
return 'break'
button = tk.Button(window,text="SelectAndCopy",command=select_all)
button.pack()
text1.focus_set()
#button.pack(fill=tk.BOTH, expand=1)
#text1.window_create(tk.INSERT,window=button)
window.mainloop()總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python代碼調(diào)用執(zhí)行shell踩坑解決
這篇文章主要為大家介紹了Python代碼調(diào)用執(zhí)行shell,踩過的坑解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
pytorch 使用單個GPU與多個GPU進行訓(xùn)練與測試的方法
今天小編就為大家分享一篇pytorch 使用單個GPU與多個GPU進行訓(xùn)練與測試的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python urlencode和unquote函數(shù)使用實例解析
這篇文章主要介紹了Python urlencode和unquote函數(shù)使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03

