欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

tkinter如何實現label超鏈接調用瀏覽器打開網址

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

tkinter label超鏈接調用瀏覽器打開網址

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

代碼:

# 首先需要導入webbrowser模塊
import webbrowser
 
# 建立窗口window
window = tk.Tk()
 
# 給窗口的可視化起名字
window.title('label超鏈接')
 
# 設置窗口的居中顯示
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)
# 設定窗口的大小(長 * 寬)
window.geometry(size)
 
# 設置label標簽
link = tk.Label(window, text='點擊購買高級版本:  www.baidu.com', font=('Arial', 10))
link.place(x=20, y=130)
 
# 此處必須注意,綁定的事件函數中必須要包含event參數
def open_url(event):
    webbrowser.open("http://www.baidu.com", new=0)
 
# 綁定label單擊時間
link.bind("<Button-1>", open_url)

python tkinter Label使用

Label使用

Label是tkinter一個比較簡單但是常用的Widget。通常用來顯示提示信息或者結果。

Label的屬性

Label的屬性有標準屬性和Widget專有屬性兩種。

標準屬性有:

    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由于標簽是不支持輸入的而無法使用。

Label方法

Label沒有專用的方法

Label屬性說明程序

程序說明

此程序說明了Label的所有屬性。可以通過下拉框選擇,查看屬性的效果以及如何設置屬性。示例如下:

Tkinter標簽屬性說明

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

窗口代碼

# 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 標簽使用說明")
    center_window(root,500,280)
    root.mainloop()


if __name__ == "__main__":
    main()

"""tkinter中關于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

第二個程序最好命名為Label_Parameter.py。

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

#coding:utf8

class Label_Parameter():
    #Label 參數名稱
    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 參數說明
    parameter_info=["背景顏色", "文本顏色", "錨定文字或者圖片", "背景顏色",
               "顯示bitmap", "邊框寬度", "同時顯示文字和圖片的方法", "光標樣式",
               "禁止時的背景顏色", "字體", "文字或者bitmap顏色", "高度",
               "圖片","多行文字時對齊方式", "x方向上內邊距", "y方向上內邊距",
               "邊框效果","狀態(tài)", "使用Tab獲得焦點", "顯示文字",
                "StringVar變量與標簽相關聯", "指定字符下劃線", "寬度", "折疊多行"]

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

    }

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

在實際的應用中不需要定義這么多的全局變量,也可以使用類的成員變量的方式減少定義全局變量的風險

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • python3多線程知識點總結

    python3多線程知識點總結

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

    python模擬enum枚舉類型的方法小結

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

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

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

    Python基礎之教你怎么在M1系統上使用pandas

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

    Python Matplotlib繪圖基礎知識代碼解析

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

    Python中的Joblib庫使用學習總結

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

    如何使用OpenCV實現手勢音量控制

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

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

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

    python+requests+pytest接口自動化的實現示例

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

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

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

最新評論