2022年11月16日 星期三

Python GUI 標籤 Label ---- EX2.17 , 2.18

 # ch2_17.py

from tkinter import *

root = Tk()

root.title("ch2_17")

label=Label(root,text="raised",relief="raised")

label.pack()

root.mainloop()


# ch2_18.py

from tkinter import *

root = Tk()

root.title("ch2_18")

label=Label(root,text="raised",relief="raised",

            bg="lightyellow",

            padx=5,pady=10)

label.pack()

root.mainloop()









Python GUI 標籤 Label L1

import tkinter as tk

win = tk.Tk()

win.title("Python Label")

win.geometry('400x200')

win.iconbitmap('t1.ico')

# 若內容是文字則以字元為單位,圖像則以圖元為單位

label = tk.Label(win, text="網址:https://alex9ufoexploer.blogspot.com ",font=('宋體',20, 'bold italic'),bg="#7CCD7C",

# 設置標籤內容區大小

width=40,height=5,

# 設置填充區距離、邊框寬度和其樣式(凹陷式)

padx=10, pady=15, borderwidth=10, relief="sunken")

label.pack()

win.mainloop()



Tkinter Label標籤控制項

 

Tkinter Label標籤控制項

Label(標籤)控制項,是 Tkinter 中最常使用的一種控制項,主要用來顯示視窗中的文本或者圖像,並且不同的 Lable(標籤)允許設置各自不同的背景圖片。

下面對 Label(標籤)的常用屬性做簡單介紹:

屬性名稱

說明

anchor

控制文本(或圖像)在 Label 中顯示的位置(方位),通過方位的英文字串縮寫(n、ne、e、se、s、sw、w、nw、center)實現定位,默認為居中(center)

bg

用來設置背景色

bd

即 borderwidth 用來指定 Label 控制項的邊框寬度,單位為圖元,預設為 2 個圖元

bitmap

指定顯示在 Label 控制項上的點陣圖,若指定了 image 參數,則該參數會被忽略

compound

控制 Lable 中文本和圖像的混合模式,若選項設置為 CENTER,則文本顯示在圖像上,如果將選項設置為 BOTTOM、LEFT、RIGHT、TOP,則圖像顯示在文本旁邊。

cursor

指定當滑鼠在 Label 上掠過的時候,滑鼠的的顯示樣式,參數值為 arrow、circle、cross、plus

disableforeground

指定當 Label 設置為不可用狀態的時候前景色的顏色

font

指定 Lable 中文本的 (字體,大小,樣式)元組參數格式,一個 Lable 只能設置一種字體

fg

設置 Label 的前景色

height/width

設置 Lable 的高度/寬度,如果 Lable 顯示的是文本,那麼單位元是文本單元,如果 Label 顯示的是圖像,那麼單位就是圖元,如果不設置,Label 會自動根據內容來計算出標籤的高度

highlightbackground

當 Label 沒有獲得焦點的時候高亮邊框的顏色,系統的預設是標準背景色

highlightcolor

指定當 Lable 獲得焦點的話時候高亮邊框的顏色,系統預設為0,不帶高亮邊框

image

指定 Label 顯示的圖片,一般是 PhotoImage、BitmapImage 的物件

justify

表示多行文本的對齊方式,參數值為 left、right、center,注意文本的位置取決於 anchor 選項

padx/pady

padx 指定 Label 水準方向上的間距(即內容和邊框間),pady 指定 Lable 水準方向上的間距(內容和邊框間的距離)

relief

指定邊框樣式,預設值是 "flat",其他參數值有 "groove"、"raised"、"ridge"、"solid"或者"sunken"

state

該參數用來指定 Lable 的狀態,預設值為"normal"(正常狀態),其他可選參數值有"active"和"disabled"

takefocus

預設值為False,如果是 True,表示該標籤接受輸入焦點

text

用來指定 Lable 顯示的文本,注意文本內可以包含分行符號

underline

給指定的字元添加底線,預設值為 -1 表示不添加,當設置為 1 時,表示給第二個文本字元添加底線。

wraplength

將 Label 顯示的文本分行,該參數指定了分行後每一行的長度,預設值為 0

1) Label控制項構成

一個控制項主要由背景和前景兩部分組成。其中背景由三部分構成分別是內容區域、填充區、邊框,這三個區域的大小通過以下屬性進行控制,如下所示:

·        width/height

·        padx/pady

·        borderwidth


下面通過一個示意圖對 Label 背景的區域構成進行說明:



圖1:Label控制群組成


邊框的寬度可以通過 borderwidth 來調整,其樣式可以通過relief來設置(默認為平的 flat);填充區的大小調整分為水準方向和垂直方向,可以使用padx和pady來調整;內容區則主要用來顯示文字或者圖片,其大小由 width/height 來控制。

 

Python GUI Clock 數位時鐘

 

from tkinter import *

from time import strftime

root = Tk()

root.geometry('500x350+300+300')

#root.iconbitmap('t1.ico')

root.title("源自於 ->C語言 中文網")

# 设置文本标签

lb = Label(root, font=("微軟雅黑", 50, "bold"), bg='#87CEEB', fg="#B452CD")

lb.pack(anchor="center", fill="both", expand=1)

# 定义一个mode标志

mode = 'time'

# 定义显示时间的函数

def showtime():

    if mode == 'time':

        #时间格式化处理   

        string = strftime("%H:%M:%S %p")

    else:

        string = strftime("%Y-%m-%d")

    lb.config(text=string)

    # 每隔 1秒钟执行time函数

    lb.after(1000, showtime)

# 定义鼠标处理事件,点击时间切换为日期样式显示

def mouseClick(event):

    global mode

    if mode == 'time':

        # 点击切换mode样式为日期样式

        mode = 'date'

    else:

        mode = 'time'

lb.bind("<Button>", mouseClick)

# 调用showtime()函数

showtime()

# 显示窗口

mainloop()



Python GUI 標籤 Label ---- EX.13, 2.14 , 2.15 , 2.16

 # ch2_13.py

from tkinter import *

root = Tk()

root.title("ch2_13")

label=Label(root,bitmap="hourglass")

label.pack()  

root.mainloop()



# ch2_14.py

from tkinter import *

root = Tk()

root.title("ch2_14")

label=Label(root,bitmap="hourglass",

            compound="left",text="我的天空")

label.pack()  

root.mainloop()


# ch2_15.py

from tkinter import *


root = Tk()

root.title("ch2_15")

label=Label(root,bitmap="hourglass",

            compound="top",text="我的天空")

label.pack()  


root.mainloop()



# ch2_16.py

from tkinter import *

root = Tk()

root.title("ch2_16")

label=Label(root,bitmap="hourglass",

            compound="center",text="我的天空")

label.pack()  

root.mainloop()





Python GUI 標籤 Label ---- EX2.7 , 2.8 , 2.9 , 2.10 , 2.11 , 2.12

 # ch2_7.py

from tkinter import *


root = Tk()

root.title("ch2_7")

label=Label(root,text="I like tkinter",

            fg="blue",bg="yellow",

            height=3,width=15,

            anchor="nw",

            wraplength = 40)

label.pack()  


root.mainloop()


# ch2_8.py

from tkinter import *


root = Tk()

root.title("ch2_8")

label=Label(root,text="I like tkinter",

            fg="blue",bg="yellow",

            height=3,width=15,

            font="Helvetica 20 bold")

label.pack()  


root.mainloop()


# ch2_9.py

from tkinter import *


root = Tk()

root.title("ch2_9")

label=Label(root,text="abcdefghijklmnopqrstuvwy",

            fg="blue",bg="lightyellow",

            wraplength=80)

label.pack()  


root.mainloop()


# ch2_10.py

from tkinter import *


root = Tk()

root.title("ch2_10")

label=Label(root,text="abcdefghijklmnopqrstuvwy",

            fg="blue",bg="lightyellow",

            wraplength=80,

            justify="left")

label.pack()  


root.mainloop()



# ch2_11.py

from tkinter import *


root = Tk()

root.title("ch2_11")

label=Label(root,text="abcdefghijklmnopqrstuvwy",

            fg="blue",bg="lightyellow",

            wraplength=80,

            justify="center")

label.pack()  


root.mainloop()


# ch2_12.py

from tkinter import *


root = Tk()

root.title("ch2_12")

label=Label(root,text="abcdefghijklmnopqrstuvwy",

            fg="blue",bg="lightyellow",

            wraplength=80,

            justify="right")

label.pack()  


root.mainloop()




Python GUI 標籤 Label ---- EX2.3 , 2.4 , 2.5 , 2.6 , 2.6.1

 # ch2_3.py

from tkinter import *

root = Tk()

root.title("ch2_3")

label=Label(root,text="I like tkinter",

            fg="blue",bg="yellow")

label.pack()  

root.mainloop()





# ch2_4.py
from tkinter import *

root = Tk()
root.title("ch2_4")
label=Label(root,text="I like tkinter",
            fg="blue",bg="yellow",
            height=3,width=15)
label.pack()  

root.mainloop()



# ch2_5.py
from tkinter import *

root = Tk()
root.title("ch2_5")
label=Label(root,text="I like tkinter",
            fg="blue",bg="yellow",
            height=3,width=15,
            anchor="nw")
label.pack()  

root.mainloop()






# ch2_6.py
from tkinter import *

root = Tk()
root.title("ch2_6")
label=Label(root,text="I like tkinter",
            fg="blue",bg="yellow",
            height=3,width=15,
            anchor="se")
label.pack()  

root.mainloop()






# ch2_6_1.py
from tkinter import *

root = Tk()
root.title("ch2_6_1")
label=Label(root,text="I like tkinter",
            fg="blue",bg="yellow",
            height=3,width=15,
            anchor=SE)
label.pack()  

root.mainloop()






  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...