python gui实例大全_python图形GUI基本示例

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

运行结果:

python gui实例大全_python图形GUI基本示例_第1张图片

示例二:

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()

运行结果:

python gui实例大全_python图形GUI基本示例_第2张图片

示例三

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()

运行结果:

python gui实例大全_python图形GUI基本示例_第3张图片

文字排版不啥样,只是展示效果而已,请原谅!

示例四

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()

运行结果:

python gui实例大全_python图形GUI基本示例_第4张图片

展示效果而已!

示例五

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()

运行结果

python gui实例大全_python图形GUI基本示例_第5张图片

python gui实例大全_python图形GUI基本示例_第6张图片

示例六

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()

运行结果:

1174766-20170710175151728-156963525.png

示例七

from tkinter import *

root = Tk()

GIRLS = ["西施", "貂蝉", "王昭君", "杨玉环"]

v = []

for girl in GIRLS:

v.append(IntVar())

b = Checkbutton(root, text=girl, variable=v[-1])

b.pack(anchor=W)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第7张图片

示例八:

from tkinter import *

root = Tk()

v = IntVar()

Radiobutton(root, text="one", variable=v, value=1).pack(anchor=W)

Radiobutton(root, text="two", variable=v, value=2).pack(anchor=W)

Radiobutton(root, text="three", variable=v, value=3).pack(anchor=W)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第8张图片

示例九

from tkinter import *

root = Tk()

LANGES = [

("python", 1),

("perl", 2),

("Ruby", 3),

("Lua", 4)

]

v = IntVar()

v.set(1)

for lang, num in LANGES:

b = Radiobutton(root, text=lang, variable=v, value=num, indicatoron=False)

b.pack(fill=X)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第9张图片

示例十:

from tkinter import *

root = Tk()

group = LabelFrame(root, text="最好的脚本语言是?", padx=5, pady=5)

group.pack(padx=10, pady=10)

LANGES = [

("python", 1),

("perl", 2),

("Ruby", 3),

("Lua", 4)

]

v = IntVar()

v.set(1)

for lang, num in LANGES:

b = Radiobutton(group, text=lang, variable=v, value=num)

b.pack(anchor=W)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第10张图片

示例十一

from tkinter import *

root = Tk()

e = Entry(root)

e.pack(padx=20, pady=20)

e.delete(0, END)

e.insert(0, "默认文本...")

mainloop()

运行结果:

1174766-20170711145644665-1350243302.png

示例十二

from tkinter import *

root = Tk()

Label(root, text="作品:").grid(row=0, column=0)

Label(root, text="作者:").grid(row=1, column=0)

e1 = Entry(root)

e2 = Entry(root)

e1.grid(row=0, column=1, pady=5)

e2.grid(row=1, column=1, pady=5)

def show():

print("作品: 《%s》" % e1.get())

print("作者: 《%s》" % e2.get())

Button(root, text="获取信息", width=10, command=show).grid(row=3, column=0, sticky=W,padx=10,pady=5)

Button(root, text="退出", width=10, command=root.quit).grid(row=3, column=1, sticky=W,padx=10,pady=5)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第11张图片

示例十三

from tkinter import *

root = Tk()

Label(root, text="账号:").grid(row=0, column=0)

Label(root, text="密码:").grid(row=1, column=0)

v1 = StringVar()

v2 = StringVar()

e1 = Entry(root, textvariable=v1)

e2 = Entry(root, textvariable=v2, show="*")

e1.grid(row=0, column=1, pady=5)

e2.grid(row=1, column=1, pady=5)

def show():

print("账号: 《%s》" % e1.get())

print("密码: 《%s》" % e2.get())

Button(root, text="获取信息", width=10, command=show).grid(row=3, column=0, sticky=W,padx=10,pady=5)

Button(root, text="退出", width=10, command=root.quit).grid(row=3, column=1, sticky=W,padx=10,pady=5)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第12张图片

示例十四:

from tkinter import *

master = Tk()

def test():

if e1.get() == 'cdx':

print("正确!")

return True

else:

print("错误!")

e1.delete(0, END)

return False

v = StringVar()

e1 = Entry(master, textvariable=v, validate="focusout", validatecommand=test)

e2 = Entry(master)

e1.pack(padx=10, pady=10)

e2.pack(padx=10, pady=10)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第13张图片

示例十五

from tkinter import *

master = Tk()

v = StringVar()

def test1():

if e1.get() == 'cdx':

print("正确!")

return True

else:

print("错误!")

e1.delete(0, END)

return False

def test2():

print("我被调用了..")

return True

e1 = Entry(master, textvariable=v, validate="focusout", validatecommand=test1,invalidcommand=test2)

e2 = Entry(master)

e1.pack(padx=10, pady=10)

e2.pack(padx=10, pady=10)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第14张图片

示例十六

from tkinter import *

master = Tk()

v = StringVar()

def test(content, reason, name):

if content == "cdx":

print("正确!")

print(content, reason, name)

return True

else:

print("错误!")

print(content, reason, name)

return False

testCMD = master.register(test)

e1 = Entry(master, textvariable=v, validate="focusout", validatecommand=(testCMD, '%P', '%v', '%W'))

e2 = Entry(master)

e1.pack(padx=10, pady=10)

e2.pack(padx=10, pady=10)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第15张图片

示例十七

from tkinter import *

master = Tk()

frame = Frame(master)

frame.pack(padx=10, pady=10)

v1 = StringVar()

v2 = StringVar()

v3 = StringVar()

def test(content):

return content.isdigit()

testCMD = master.register(test)

e1 = Entry(frame, width=10, textvariable=v1, validate="key", validatecommand=(testCMD,'%P')).grid(row=0, column=0)

Label(frame, text="+").grid(row=0, column=1)

e2 = Entry(frame, width=10, textvariable=v2, validate="key", validatecommand=(testCMD,'%P')).grid(row=0, column=2)

Label(frame, text="+").grid(row=0, column=3)

e3 = Entry(frame, width=10, textvariable=v3, state="readonly").grid(row=0, column=4)

def calc():

result = int(v1.get()) + int(v2.get())

v3.set(result)

Button(frame, text="计算结果", command=calc).grid(row=1, column=2, pady=5)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第16张图片

示例十八

from tkinter import *

master = Tk()

theLB = Listbox(master, selectmode=EXTENDED)#selectmode=SINGLE单选 selectmode=EXTENDED多选

theLB.pack()

for item in ["鸡蛋", "鸭蛋", "鹅蛋", "李狗蛋"]:

theLB.insert(END,item)

# theLB.delete(1)

theButton = Button(master, text='删除它', command=lambda x=theLB:x.delete(ACTIVE))

theButton.pack()

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第17张图片

示例十九

from tkinter import *

master = Tk()

theLB = Listbox(master, selectmode=EXTENDED, height=8)#selectmode=SINGLE单选 selectmode=EXTENDED多选

theLB.pack()

for item in range(11):

theLB.insert(END,item)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第18张图片

示例二十

from tkinter import *

root = Tk()

sb = Scrollbar(root)

sb.pack(side=RIGHT, fill=Y)

lb = Listbox(root, yscrollcommand=sb.set)

for i in range(1000):

lb.insert(END, i)

lb.pack(side=LEFT, fill=BOTH)

sb.config(command=lb.yview)

mainloop()

运行结果:

python gui实例大全_python图形GUI基本示例_第19张图片

你可能感兴趣的:(python,gui实例大全)