【注: 以下示例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显示画面需要什么 python3图形界面_控件

示例二:

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显示画面需要什么 python3图形界面_Python显示画面需要什么_02

示例三

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显示画面需要什么 python3图形界面_控件_03

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

示例四

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显示画面需要什么 python3图形界面_python_04

展示效果而已!

示例五

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显示画面需要什么 python3图形界面_控件_05

Python显示画面需要什么 python3图形界面_控件_06

示例六

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显示画面需要什么 python3图形界面_pythongui 三维_07

示例七

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显示画面需要什么 python3图形界面_python_08

示例八:

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显示画面需要什么 python3图形界面_Python显示画面需要什么_09

示例九

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显示画面需要什么 python3图形界面_pythongui 三维_10

示例十:

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显示画面需要什么 python3图形界面_pythongui 三维_11

示例十一

from tkinter import *
root = Tk()
e = Entry(root)
e.pack(padx=20, pady=20)
e.delete(0, END)
e.insert(0, "默认文本...")
mainloop()

运行结果:

Python显示画面需要什么 python3图形界面_ide_12

示例十二

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显示画面需要什么 python3图形界面_Python显示画面需要什么_13

示例十三

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显示画面需要什么 python3图形界面_控件_14

示例十四:

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显示画面需要什么 python3图形界面_ide_15

示例十五

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显示画面需要什么 python3图形界面_控件_16

示例十六

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显示画面需要什么 python3图形界面_控件_17

示例十七

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显示画面需要什么 python3图形界面_控件_18

示例十八

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显示画面需要什么 python3图形界面_pythongui 三维_19

示例十九

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显示画面需要什么 python3图形界面_Python显示画面需要什么_20

示例二十

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显示画面需要什么 python3图形界面_ide_21