亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

tkinter如何實(shí)現(xiàn)label超鏈接調(diào)用瀏覽器打開網(wǎng)址

 更新時(shí)間:2023年01月28日 15:10:01   作者:烏拉隊(duì)長(zhǎng)  
這篇文章主要介紹了tkinter如何實(shí)現(xiàn)label超鏈接調(diào)用瀏覽器打開網(wǎng)址問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

tkinter label超鏈接調(diào)用瀏覽器打開網(wǎng)址

tkinter的label標(biāo)簽沒有command屬性,但是可以利用bind方法給label標(biāo)簽綁定鼠標(biāo)和鍵盤事件

代碼:

# 首先需要導(dǎo)入webbrowser模塊
import webbrowser
 
# 建立窗口window
window = tk.Tk()
 
# 給窗口的可視化起名字
window.title('label超鏈接')
 
# 設(shè)置窗口的居中顯示
screenwidth = window.winfo_screenwidth()
screenheight = window.winfo_screenheight()
width = 700
height = 150
size = "%dx%d+%d+%d" % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
# 設(shè)定窗口的大小(長(zhǎng) * 寬)
window.geometry(size)
 
# 設(shè)置label標(biāo)簽
link = tk.Label(window, text='點(diǎn)擊購買高級(jí)版本:  www.baidu.com', font=('Arial', 10))
link.place(x=20, y=130)
 
# 此處必須注意,綁定的事件函數(shù)中必須要包含event參數(shù)
def open_url(event):
    webbrowser.open("http://www.baidu.com", new=0)
 
# 綁定label單擊時(shí)間
link.bind("<Button-1>", open_url)

python tkinter Label使用

Label使用

Label是tkinter一個(gè)比較簡(jiǎn)單但是常用的Widget。通常用來顯示提示信息或者結(jié)果。

Label的屬性

Label的屬性有標(biāo)準(zhǔn)屬性和Widget專有屬性兩種。

標(biāo)準(zhǔn)屬性有:

    activebackground, activeforeground, anchor,
    background, bitmap, borderwidth, cursor,
    disabledforeground, font, foreground,
    highlightbackground, highlightcolor,
    highlightthickness, image, justify,
    padx, pady, relief, takefocus, text,
    textvariable, underline, wraplength

其中highlightbackground, highlightcolor,highlightthickness和takefoucs由于標(biāo)簽是不支持輸入的而無法使用。

Label方法

Label沒有專用的方法

Label屬性說明程序

程序說明

此程序說明了Label的所有屬性??梢酝ㄟ^下拉框選擇,查看屬性的效果以及如何設(shè)置屬性。示例如下:

Tkinter標(biāo)簽屬性說明

代碼由兩部分組成,第一部分是Tkinter窗口代碼,第二部分是Label屬性數(shù)據(jù)。

窗口代碼

# coding:utf8

import tkinter as tk
from tkinter.ttk import *
from Label_Parameter import *

cbx_para = None  # 屬性下拉框
cbx_method = None  # 方法下拉框
lbl_status = None  # 輸出說明性文字
lbl_code = None
lbl_result = None
frm_code = None
frm_result = None
init_para={}
demo_image = None
v_str = None

def GetInitParameter():
    global lbl_result
    global init_para
    init_para={}
    for item in Label_Parameter.parameter:
        index = item.split("/")[0]
        init_para[index] = lbl_result[index]

def ClearParameter():
    global lbl_result
    global init_para
    for item in Label_Parameter.parameter:
        index = item.split("/")[0]
        lbl_result[index]=init_para[index]


def Para_Demo(*args):
    global cbx_para
    global lbl_code
    global lbl_status
    global lbl_result
    global frm_result
    global frm_code
    global demo_image
    global v_str

    index = cbx_para.current()

    #
    if index in Label_Parameter.Label_Para:
        ClearParameter()
        frm_code.grid(row=3, column=1, padx=5, pady=5)
        frm_result.grid(row=3, column=2, padx=5, pady=5)

        frm_code["text"]=Label_Parameter.parameter[index]+":代碼"
        frm_result["text"]=Label_Parameter.parameter[index]+":效果"
        temp = Label_Parameter.Label_Para[index]
        dis_code = ""
        for item in range(1,len(temp[0])):
            dis_code = dis_code+temp[0][item]+"\n"

        lbl_code['text'] = dis_code
        for item in range(1,len(temp[0])):
            exec(temp[0][item])
        # exec(item) for item in Label_Para[index][2]
        lbl_status['text'] = temp[1]
    else:
        frm_code.grid_forget()
        frm_result.grid_forget()

def center_window(root, width, height):
    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
    root.geometry(size)

def main():
    root = tk.Tk()
    # 屬性下拉框
    # 按鈕
    global frm_code
    global frm_result
    global v_str
    v_str = tk.StringVar()

    frm_top = tk.Frame(root)
    frm_top.grid(row=1, column=1, sticky="W", columnspan=3, pady=2)
    sp = Separator(root, orient='horizontal')
    sp.grid(row=2, column=1, columnspan=2, sticky="nwse")
    frm_code = tk.LabelFrame(root,text="")
    frm_code.grid(row=3, column=1, padx=10)
    sp = Separator(root, orient='vertical')
    sp.grid(row=3, column=2, sticky="nwse")
    frm_result = tk.LabelFrame(root,text="")
    frm_result.grid(row=3, column=2, pady=5)
    sp = Separator(root, orient='horizontal')
    sp.grid(row=4, column=1, columnspan=2, sticky="nwse")
    frm_bottom = tk.Frame(root)
    frm_bottom.grid(row=5, column=1, columnspan=3, sticky="w")

    nCbx_Height = 10
    tk.Label(frm_top, text="屬性:").grid(row=1, column=1, sticky="e")
    global cbx_para

    txt = [Label_Parameter.parameter[item] + ":" + Label_Parameter.parameter_info[item]
           for item in range(len(Label_Parameter.parameter))]
    cbx_para = Combobox(frm_top, height=nCbx_Height, values=txt, width=38)
    cbx_para.bind("<<ComboboxSelected>>", Para_Demo)
    cbx_para.set(txt[0])
    cbx_para.grid(row=1, column=2)

    tk.Label(frm_top, text="方法:").grid(row=1, column=3, sticky="e", padx=10)
    tk.Label(frm_top, text="無").grid(row=1, column=4, sticky="w", padx=5)
    global lbl_status
    lbl_status = tk.Label(frm_bottom, text="", anchor="w", justify="left")
    lbl_status.grid(row=1, column=1, sticky="w")

    global lbl_code
    lbl_code = tk.Label(frm_code, text="", justify="left")
    lbl_code.grid(row=1, column=1)

    global lbl_result
    lbl_result = tk.Label(frm_result, text="", justify="left")
    lbl_result.grid(row=1, column=1)

    GetInitParameter()
    global demo_image
    demo_image = tk.PhotoImage(file='a.PNG')
    frm_result.grid_forget()
    frm_code.grid_forget()

    root.title("Tkinter 標(biāo)簽使用說明")
    center_window(root,500,280)
    root.mainloop()


if __name__ == "__main__":
    main()

"""tkinter中關(guān)于Label的注釋
Construct a label widget with the parent MASTER.

STANDARD OPTIONS

    activebackground, activeforeground, anchor,
    background, bitmap, borderwidth, cursor,
    disabledforeground, font, foreground,
    highlightbackground, highlightcolor,
    highlightthickness, image, justify,
    padx, pady, relief, takefocus, text,
    textvariable, underline, wraplength

WIDGET-SPECIFIC OPTIONS

    height, state, width

"""

Label_Parameter.py

第二個(gè)程序最好命名為L(zhǎng)abel_Parameter.py。

如果命名為其他程序需要修改第一部分第5行的代碼。

#coding:utf8

class Label_Parameter():
    #Label 參數(shù)名稱
    parameter=["activebackground", "activeforeground", "anchor", "background/bg",
               "bitmap", "borderwidth/bd", "compound", "cursor",
               "disabledforeground", "font", "foreground/fg", "height",
               "image","justify", "padx", "pady",
               "relief","state", "takefocus", "text",
               "textvariable", "underline", "width", "wraplength"]
    #Label 參數(shù)說明
    parameter_info=["背景顏色", "文本顏色", "錨定文字或者圖片", "背景顏色",
               "顯示bitmap", "邊框?qū)挾?, "同時(shí)顯示文字和圖片的方法", "光標(biāo)樣式",
               "禁止時(shí)的背景顏色", "字體", "文字或者bitmap顏色", "高度",
               "圖片","多行文字時(shí)對(duì)齊方式", "x方向上內(nèi)邊距", "y方向上內(nèi)邊距",
               "邊框效果","狀態(tài)", "使用Tab獲得焦點(diǎn)", "顯示文字",
                "StringVar變量與標(biāo)簽相關(guān)聯(lián)", "指定字符下劃線", "寬度", "折疊多行"]

    #Label 說明,包括序號(hào)、代碼說明、代碼、運(yùn)行效果
    Label_Para = {
        0: [["activebackground:","lbl_result['activebackground']='red'","lbl_result['state']='active'",
                "lbl_result['text']='activebackground:背景顏色'"],
            "activebackground:定義標(biāo)簽在active狀態(tài)下背景顏色。\n需要注意的是,必須顯式定義state='active',否則該屬性不生效"],
        1: [["activeforeground:","lbl_result['activeforeground'] = 'blue'", "lbl_result['state'] = 'active'",
                "lbl_result['text']='activeforeground:文本顏色'"],
            "activeforeground:定義標(biāo)簽在active狀態(tài)下文字的顏色。\n需要注意的是,必須顯式定義state='active',否則該屬性不生效"],
        2:[["anchor:","lbl_result['height']=5","lbl_result['anchor']='se'","lbl_result['width']=30",
                "lbl_result['text']='anchor:在右下角SE顯示文本'"],
           "anchor:指定如何對(duì)齊文字或者圖片\nanchor與justify是不同的。\njustify針對(duì)的是多行文本的對(duì)齊\nanchor針對(duì)的是文本在Frame中放置的位置\nanchor可以設(shè)置為n,s,w,e或者組合,也可以是center"],
        3:[["background/bg:","lbl_result['text']='綠色背景'","lbl_result['bg']='green'",
                "lbl_result['text']='bg:背景顏色是綠色'"],
           "background/bg:指定標(biāo)簽的背景顏色"],
        4:[["bitmap:","lbl_result['bitmap']='error'"],
           "bitmap:顯示位圖\n1)使用系統(tǒng)自帶的bitmap,直接輸入名字即可。比如bitmap='error'\n2)使用自定義的bitmap,在路徑前面加上'@'即可"
           ],
        5:[["borderwidth/bd:","lbl_result['bd']=20","lbl_result['bg']='lightblue'","lbl_result['text']='bd:邊框=20'"],
           "bd:設(shè)置文本到Label邊界的寬度\nbd參數(shù)相當(dāng)于設(shè)置了padx和pady參數(shù)"
           ],
        6:[["compound:","#demo_image = tk.PhotoImage(file='a.PNG')","lbl_result['image']=demo_image","lbl_result['text']='文字在圖片下方'","lbl_result['compound']=tk.TOP"],
           "compound:設(shè)置圖片相對(duì)文字的位置\n可以設(shè)置為bottom,top,center,right,left\nbottom代表的是圖片相對(duì)文字的位置\n實(shí)際使用中要去掉第一行代碼的注釋"
           ],
        7:[["cursor:","lbl_result['cursor']='watch'","lbl_result['text']='鼠標(biāo)經(jīng)過標(biāo)簽時(shí)的光標(biāo)'"],
           "cursor:鼠標(biāo)經(jīng)過標(biāo)簽時(shí)的光標(biāo)"
           ],
        8:[["disabledforeground:","lbl_result['state']=tk.DISABLED","lbl_result['disabledforeground']='red'","lbl_result['text']='標(biāo)簽禁止?fàn)顟B(tài),文字是紅色的'"],
            "disabledforeground:標(biāo)簽被禁止的時(shí)候的文本顏色\n一定要用state=tk.DISABLED,才能使該選項(xiàng)起作用"],
        9:[["font:","lbl_result['font']=('宋體',20,)","lbl_result['text']='font=(\"宋體\",20,)'"],
           "font:設(shè)置文本字體。\n一個(gè)標(biāo)簽只能用一種字體。如果要用多種字體,需要用多個(gè)標(biāo)簽\n字體定義可以使用三元組(字體名稱,大小,修飾)\n也可以使用Font類"
           ],
        10:[["foreground/fg:","lbl_result['text']='紅色文本'","lbl_result['fg']='red'"],
            "fg:設(shè)置標(biāo)簽文本的顏色\n要求標(biāo)簽的狀態(tài)是tk.NORMAL"],
        11:[["height:","lbl_result['text']='height=10'","lbl_result['height']=10"],
            "height:設(shè)置標(biāo)簽的高度,一般與width配合使用"],
        12: [["image:", "#demo_image = tk.PhotoImage(file='a.PNG')","lbl_result['image']=demo_image"],
             "image:顯示圖片"],
        13: [["justify", "lbl_result['justify']=tk.LEFT", "lbl_result['text']='文字左對(duì)齊\\n第二行'"],
             "justify:多行文本的對(duì)齊方式\n支持left,right,center"],
        14: [["padx:", "lbl_result['text']='padx=5'", "lbl_result['width']=30","lbl_result['padx']=5"],
             "padx:設(shè)置文本到標(biāo)簽邊框水平方向距離,常與pady配合使用"],
        15:[["pady:","lbl_result['text']='pady=5'","lbl_result['pady']=5"],
            "pady:設(shè)置文本到標(biāo)簽邊框垂直方向的距離,常與padx配合使用"],
        16:[["relief:","lbl_result['text']='邊框修飾效果'","lbl_result['bd']=10","lbl_result['relief']='raised'"],
            "relief:標(biāo)簽邊框的修飾效果\n可設(shè)為flat, groove, raised, ridge, solid, or sunken"],
        17:[["state","lbl_result['state']=tk.NORMAL","lbl_result['text']='標(biāo)簽正常狀態(tài)'"],
            "state:設(shè)置標(biāo)簽狀態(tài)\n可設(shè)為active, disabled, normal"],
        18:[["takefocus:","lbl_result['takefocus']=True","lbl_result['text']='標(biāo)簽獲得輸入焦點(diǎn)'"],
            "takefocus:獲得輸入焦點(diǎn)。\n由于Label是不支持編輯的,所以獲得輸入焦點(diǎn)沒有作用"],
        19: [["text:", "lbl_result['text']='標(biāo)簽顯示的文本'"],
             "text:為標(biāo)簽要顯示的文本"],
        20:[["textvariable:","v_str.set('通過textvariable改變標(biāo)簽文本')","lbl_result['textvariable']=v_str"],
             "textvariable:定義與標(biāo)簽文本相關(guān)聯(lián)的變量\n該變量的變化會(huì)反應(yīng)在標(biāo)簽中"],
        21: [["underline:","lbl_result['underline']=4","lbl_result['text']='Hello World!'"],
             "underline:在指定字母下面顯示下劃線"],
        22: [["width:","lbl_result['width']=30","lbl_result['text']='width=30'"],
             "width:定義標(biāo)簽寬度\n常與height屬性配合使用"],
        23: [["wraplength:","lbl_result['wraplength']='50'","lbl_result['text']='這個(gè)很長(zhǎng)的文本會(huì)被折行顯示wraplength=30'"],
             "wraplength:定義折行顯示\n超過wraplength定義的長(zhǎng)度的文字會(huì)折行顯示\nwraplength的單位是像素,不是文字?jǐn)?shù)目"]

    }

第一部分程序使用了很多的全局變量,是為了編程以及示例代碼說明的需要。

在實(shí)際的應(yīng)用中不需要定義這么多的全局變量,也可以使用類的成員變量的方式減少定義全局變量的風(fēng)險(xiǎn)

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python3多線程知識(shí)點(diǎn)總結(jié)

    python3多線程知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給各位分享的是關(guān)于python3多線程的相關(guān)知識(shí)點(diǎn)內(nèi)容,以后需要的朋友們可以參考下。
    2019-09-09
  • python模擬enum枚舉類型的方法小結(jié)

    python模擬enum枚舉類型的方法小結(jié)

    這篇文章主要介紹了python模擬enum枚舉類型的方法,實(shí)例總結(jié)了python模擬enum枚舉類型的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • Python中的三目(元)運(yùn)算符詳解

    Python中的三目(元)運(yùn)算符詳解

    這篇文章主要介紹了python 三元運(yùn)算符使用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Python基礎(chǔ)之教你怎么在M1系統(tǒng)上使用pandas

    Python基礎(chǔ)之教你怎么在M1系統(tǒng)上使用pandas

    這篇文章主要介紹了Python基礎(chǔ)之教你怎么在M1系統(tǒng)上使用pandas,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • Python Matplotlib繪圖基礎(chǔ)知識(shí)代碼解析

    Python Matplotlib繪圖基礎(chǔ)知識(shí)代碼解析

    這篇文章主要介紹了Python Matplotlib繪圖基礎(chǔ)知識(shí)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Python中的Joblib庫使用學(xué)習(xí)總結(jié)

    Python中的Joblib庫使用學(xué)習(xí)總結(jié)

    這篇文章主要介紹了Python中的Joblib庫使用學(xué)習(xí)總結(jié),Joblib是一組在Python中提供輕量級(jí)流水線的工具,Joblib已被優(yōu)化得很快速,很健壯了,特別是在大數(shù)據(jù)上,并對(duì)numpy數(shù)組進(jìn)行了特定的優(yōu)化,需要的朋友可以參考下
    2023-08-08
  • 如何使用OpenCV實(shí)現(xiàn)手勢(shì)音量控制

    如何使用OpenCV實(shí)現(xiàn)手勢(shì)音量控制

    今天來學(xué)習(xí)一下如何使用OpenCV實(shí)現(xiàn)手勢(shì)音量控制,本次實(shí)驗(yàn)需要使用OpenCV和mediapipe庫進(jìn)行手勢(shì)識(shí)別,并利用手勢(shì)距離控制電腦音量,感興趣的朋友跟隨小編一起看看吧
    2023-11-11
  • Python異步之迭代器如何使用詳解

    Python異步之迭代器如何使用詳解

    這篇文章主要為大家介紹了Python異步之迭代器如何使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • python+requests+pytest接口自動(dòng)化的實(shí)現(xiàn)示例

    python+requests+pytest接口自動(dòng)化的實(shí)現(xiàn)示例

    這篇文章主要介紹了python+requests+pytest接口自動(dòng)化的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 使用Python創(chuàng)建一個(gè)視頻管理器并實(shí)現(xiàn)視頻截圖功能

    使用Python創(chuàng)建一個(gè)視頻管理器并實(shí)現(xiàn)視頻截圖功能

    在這篇博客中,我將向大家展示如何使用 wxPython 創(chuàng)建一個(gè)簡(jiǎn)單的圖形用戶界面 (GUI) 應(yīng)用程序,該應(yīng)用程序可以管理視頻文件列表、播放視頻,并生成視頻截圖,我們將逐步實(shí)現(xiàn)這些功能,并確保代碼易于理解和擴(kuò)展,感興趣的小伙伴跟著小編一起來看看吧
    2024-08-08

最新評(píng)論