標籤:矩形地區 var imp and comm hello roo 文本 alt
【注: 以下樣本python3版本】
樣本一:
import tkinter as tk
app = tk.Tk()
app.title("FishC Demo")
theLable = tk.Label(app, text="我的第二個視窗程序",width=20,height=10)#標籤控制項;可以顯示文本和位元影像
theLable.pack()
app.mainloop()
運行結果:
樣本二:
import tkinter as tk
class App: #定義類
def __init__(self, master):
frame = tk.Frame(master)#定義表單架構
frame.pack(side=tk.RIGHT, padx=100,pady=100)
#定義按鈕
self.hi_there = tk.Button(frame, text="打招呼",bg="red" ,fg=‘green‘,command=self.say_hi)
self.hi_there.pack(padx=10, pady=10)#位置
def say_hi(self):
print("Hi! 大家好,終於我跟大家見面了。")
root = tk.Tk()
app = App(root)
root.mainloop()
運行結果:
樣本三
from tkinter import *
root = Tk()
textLabel = Label(root,text="有些心疼,又帶著欣喜。\n她那麼簡單,不好看,卻努力的開著,\
\n為了迎合清風掠心的柔情,一直努力的開著,\n半盞清歌的招搖,\
肆意蔥蘢。\n而我,恰巧路過。於是,\n喜意便盈盈的生了起來,帶\
著異樣的溫暖和樸素,\n將心,都染了濕濕的綠,盎然的,\n萬千端倪。", justify=LEFT, padx=10) #Label 標籤控制項;可以顯示文本和位元影像
textLabel.pack(side=LEFT)
photo = PhotoImage(format="png",file=r"C:\Users\Administrator\Desktop\123 (2).png")#PhotoImagecan be used for GIF and PPM/PGM color bitmaps
imgLabel = Label(root, image=photo)
imgLabel.pack(side=RIGHT)
mainloop()
運行結果:
文字排版不啥樣,只是展示效果而已,請原諒!
樣本四
from tkinter import *
root = Tk()
photo = PhotoImage(file=r"C:\Users\Administrator\Desktop\123.png")
theLable = Label(root,
text=‘Hello‘,
justify = LEFT,
image=photo,
compound=CENTER,
font=("黑體",20),
fg = "red")
theLable.pack()
mainloop()
運行結果:
展示效果而已!
樣本五
from tkinter import *
def callback():
var.set(‘吹吧你,我才不信呢‘)
root = Tk()
frame1 = Frame(root)#Frame 架構控制項;在螢幕上顯示一個矩形地區,多用來作為容器
frame2 = Frame(root)
var = StringVar() #設定字串
var.set("你所下載的影片含有末成人限制內容,\n請滿18周歲後再點擊觀看")
textLabel = Label(frame1,
textvariable = var,
justify = LEFT)
textLabel.pack(side=LEFT)
photo = PhotoImage(file=r"C:\Users\Administrator\Desktop\123 (2).png")
imgLabel = Label(frame1,text="圖片一", image=photo)
imgLabel.pack(side=RIGHT)
theButton = Button(frame2,text="我已滿18周歲", command=callback) #定義一個按鈕
theButton.pack()
frame1.pack(padx=10, pady=10)#定義位置
frame2.pack(padx=50, pady=50)
mainloop()
運行結果
樣本六
from tkinter import *
root = Tk()
frame = Frame(root)
v = IntVar()
c = Checkbutton(root, text="測試一下", variable=v)#定義一個複選框
c.pack()
l = Label(root, textvariable=v)
l.pack()
if __name__ == ‘__main__‘:
mainloop()
運行結果:
樣本七
python圖形GUI基本樣本