【注: 以下示例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()
运行结果: