pythongui实例⼤全_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()
运⾏结果:
⽰例⼆:
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()root的初始密码
运⾏结果:
⽰例七
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()
运⾏结果:
⽰例⼋:
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()
运⾏结果:
⽰例九
from tkinter import *
root = Tk()
LANGES = [
("python", 1),
("perl", 2),
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论