pythongui 三维_python图形GUI基本示例

本文通过19个示例详细介绍了Tkinter库在创建GUI应用程序中的使用,包括标签、按钮、输入框、复选框、单选按钮、滚动条、列表框等组件的创建与交互,以及文本验证和事件处理。这些示例涵盖了基本的图形用户界面设计,为Python初学者提供了实践参考。
摘要由CSDN通过智能技术生成

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

运行结果:

1174766-20170710170115743-1614506674.png

示例二:

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

运行结果:

1174766-20170710173205134-628499905.png

示例三

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

运行结果:

1174766-20170710173816290-1152428001.png

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

示例四

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

运行结果:

1174766-20170710174349493-1072511565.png

展示效果而已!

示例五

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

运行结果

1174766-20170710174719509-1919767208.png

1174766-20170710174745587-1384185036.png

示例六

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

运行结果:

1174766-20170711144841931-390578326.png

示例八:

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

运行结果:

1174766-20170711145048384-1762484248.png

示例九

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

运行结果:

1174766-20170711145312493-1739716498.png

示例十:

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

运行结果:

1174766-20170711145442665-391710053.png

示例十一

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

运行结果:

1174766-20170711145828197-944569978.png

示例十三

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

运行结果:

1174766-20170711150352556-1973291922.png

示例十四:

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

运行结果:

1174766-20170711150615587-1495604095.png

示例十五

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

运行结果:

1174766-20170711151609759-1707011979.png

示例十六

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

运行结果:

1174766-20170711151809478-1921116845.png

示例十七

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

运行结果:

1174766-20170711152119290-2040626117.png

示例十八

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

运行结果:

1174766-20170711152806150-1621922998.png

示例十九

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

运行结果:

1174766-20170711153027759-1289219877.png

示例二十

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

运行结果:

1174766-20170711153124259-1561882413.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值