import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.hi_there = tk.Button(self)
self.hi_there["text"] = "Hello World\n(click me)"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red",
command=self.master.destroy)
self.quit.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()
2、列表+按钮绑定使用,class结构根据官方。
from tkinter import *
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.master = master
self.master.title('999')
self.create_widgets()
def create_widgets(self):
sb = Scrollbar(self)
sb.pack(side=RIGHT, fill=Y)
lb = Listbox(self, yscrollcommand=sb.set)
lb.pack(side=LEFT, fill=Y)
for i in range(1000):
lb.insert(END, f'狗蛋{i}号')
sb.config(command=lb.yview)
btn = Button(self.master, text='删除', command=lambda x=lb: x.delete(ACTIVE))
btn.pack(side=BOTTOM)
if __name__ == '__main__':
root = Tk()
app = Application(master=root)
app.mainloop()
from tkinter import *
class App:
def __init__(self, master):
self.create_widgets(master)
def create_widgets(self, master):
frame = Frame(master)
frame.pack(side=LEFT, padx=10, pady=10)
btn = Button(frame, text='打招呼', fg='#fff', bg='#000',
command=lambda: print('99'))
btn.pack()
if __name__ == '__main__':
root = Tk()
root.title('Tk-first')
app = App(root)
root.mainloop()
from tkinter import *
class App:
def __init__(self, master):
self.create_widgets(master)
def create_widgets(self, master):
t = Label(master,
text='今日新鲜事为您提供最新今天新鲜事儿,\n24小时不间断呈现国内新鲜事',
justify=LEFT,
padx=10)
t.pack(side=LEFT)
# pic = PhotoImage(file='')
photo = Label(master,
# image=pic,
bg='blue',
padx=10,
width=5,
# height=5
)
photo.pack(side=RIGHT)
if __name__ == '__main__':
root = Tk()
root.title('Tk-first')
app = App(root)
root.mainloop()
from tkinter import *
class App:
def __init__(self, master):
self.create_widgets(master)
def create_widgets(self, master):
self.pic = PhotoImage(file='2015_Infotainment_06_v2.png')
t = Label(master,
text='AutoDesk Maya 2020',
justify=LEFT,
image=self.pic,
compound=CENTER,
font=('微软雅黑', 20),
fg='#fff'
)
t.pack()
if __name__ == '__main__':
root = Tk()
root.title('Tk-first')
app = App(root)
root.mainloop()
tk中图片要注意放在 全局变量 或者作为 类的属性,不能在函数中单独定i一,否则不显示。
5、获取对象改字需要使用 StringVar() 对象设置可变的文本。
from tkinter import *
class App:
def __init__(self, master):
self.create_widgets(master)
def create_widgets(self, master):
f1, f2 = Frame(master), Frame(master)
for i in [f1, f2]:
i.pack(padx=10, pady=10)
var = StringVar()
var.set('今日新鲜事为您提供最新今天新鲜事儿,\n24小时不间断呈现国内新鲜事')
t = Label(f1,
textvariable=var,
justify=LEFT,
padx=10)
t.pack(side=LEFT)
# pic = PhotoImage(file='')
photo = Label(f1,
# image=pic,
bg='blue',
padx=10,
width=5,
# height=5
)
photo.pack(side=RIGHT)
def calback1():
var.set('吹吧你,我才不信呢!')
btn = Button(f2,
text='我18岁了啦!',
command=calback1
)
btn.pack()
if __name__ == '__main__':
root = Tk()
root.title('Tk-first')
app = App(root)
root.mainloop()
6、CheckButton的使用和对齐时使用anchor=W(左西右东)
from tkinter import *
class App:
def __init__(self, master):
# self.test(master)
# self.create_widgets(master)
# self.sinGleCh(master)
self.morEleCh(master)
def test(self, master):
v = IntVar()
c = Checkbutton(master, text='测试一下', variable=v)
c.pack()
l = Label(master, textvariable=v)
l.pack()
def create_widgets(self, master):
girls = ['西施', '貂蝉', '王昭君', '杨玉环']
for i in girls:
b = Checkbutton(master, text=i, variable=IntVar())
b.pack(anchor=W)
def sinGleCh(self, master):
o = IntVar()
for k, v in {'one': 1, 'two': 2, 'three': 3, 'Python': 4}.items():
Radiobutton(master, text=k, variable=o,
value=v,
indicatoron=False).pack(anchor=W, fill=X)
def morEleCh(self, master):
group = LabelFrame(master, text='最好的脚本语言是?',
padx=5, pady=5
)
group.pack(padx=10, pady=10)
langs = [
('Python', 1),
('Perl', 2),
('Ruby', 3),
('Lua', 4),
]
v = IntVar()
for lang, num in langs:
Radiobutton(group, text=lang, variable=v, value=num).pack(anchor=W)
if __name__ == '__main__':
root = Tk()
root.title('Tk-Checkbutton')
app = App(root)
root.mainloop()
图三加入了Button的 indicatoron=False,和pack(fill=X)的方法形成了图四。最后一个图是使用了LabelFrame。
LabeFrame中的 pad 是 LabeFrame 和 Radiobutton 之间的距离。
from tkinter import *
class App:
def __init__(self, master):
self.create_widgets(master)
# self.sCale(master)
def create_widgets(self, master):
f1, f2 = Frame(master), Frame(master)
f1.pack(side=TOP, fill=Y, padx=5, pady=5)
f2.pack(side=BOTTOM, fill=X, padx=5, pady=5)
sb = Scrollbar(f1)
sb.pack(side=RIGHT, fill=Y)
lb = Listbox(f1, selectmode=EXTENDED, yscrollcommand=sb.set)
lb.pack(side=LEFT, fill=Y)
for i in range(1000):
lb.insert(END, f'狗蛋{i}号')
sb.config(command=lb.yview)
btn = Button(f2, text='删除',
bg='#fff',
command=lambda x=lb: x.delete(ACTIVE)).pack(fill=X)
def sCale(self, master):
s1 = Scale(master, from_=0, to=42, tickinterval=5, resolution=5, length=200)
s1.pack()
s2 = Scale(master, from_=0, to=200, orient=HORIZONTAL,
tickinterval=10, length=600)
s2.pack()
def show():
print(s1.get(), s2.get())
Button(master, text='当前位置', command=show).pack()
if __name__ == '__main__':
root = Tk()
root.title('Tk-Checkbutton')
app = App(root)
root.mainloop()
from tkinter import *
import math as m
class App:
def __init__(self, master):
# self.Canvas1(master)
self.CanvasP(master)
def Canvas1(self, master):
# 创建Canvas
w = Canvas(master, width=200, height=100, bg='#fff')
w.pack()
# 创建图形
line1 = w.create_line(0, 50, 200, 50, fill='#22F633')
line2 = w.create_line(100, 0, 100, 100, fill='red', dash=(4, 4))
rect1 = w.create_rectangle(50, 25, 150, 75, fill='blue')
# 移动图形
w.coords(line1, 0, 25, 200, 25)
w.itemconfig(rect1, fill='red')
w.delete(line2)
# 椭圆
w.create_oval(50, 25, 150, 75, fill='pink')
# 创建多边形
cx, cy, r = 100, 50, 40
r1, r2 = int(r * m.sin(2 * m.pi / 5)), int(r * m.cos(2 * m.pi / 5))
r3, r4 = int(r * m.sin(m.pi / 5)), int(r * m.cos(m.pi / 5))
points = [
cx - r1, cy - r2,
cx + r1, cy - r2,
cx - r3, cy + r4,
cx, cy - r,
cx + r3, cy + r4,
]
w.create_polygon(points, outline='green', fill='yellow')
w.create_oval(cx - r, cy - r, cx + r, cy + r, outline='blue', width=2)
# 文字
w.create_text(100, 50, text='Harry')
# 按钮
Button(master, text='删除全部', command=lambda x=ALL: w.delete(x)).pack()
def CanvasP(self, master):
f1, f2 = Frame(master), Frame(master)
f1.pack(side=TOP, fill=X, padx=5, pady=5)
f2.pack(side=BOTTOM, fill=X, padx=5, pady=5)
w = Canvas(f1, width=400, height=200, bg='#fff')
w.pack()
def paint(event):
x1, y1 = (event.x - 1), (event.y - 1)
x2, y2 = (event.x + 1), (event.y + 1)
w.create_oval(x1, y1, x2, y2, fill='red')
w.bind('' , paint)
Label(f2, text='写下你的留言:...').pack(side=LEFT)
Button(f2, text='清空', command=lambda x=ALL: w.delete(x)).pack(side=RIGHT)
if __name__ == '__main__':
root = Tk()
root.title('Tk-Canvas')
app = App(root)
root.mainloop()