创建窗口
有错误哦!
#创建Frame的子类来创建窗口
from tkinter import *
class Application(Frame):
def __init__(self,master = None):
Frame.__init__(self,master)
self.pack()
self.initWidgets()
def initWidgets(self):
w = Label(self)
bm = PhotoImage(file = 'serial.png')
w.x = bm
w['image'] = bm
w.pack()
okButton = Button(self,text = '确定')
okButton['background'] = 'yellow'
okButton.pack()
app = Application()
print(type(app.master))
app.master.title('窗口标题')
app.mainloop()
#使用多个容器(Frame)分开布局,再将Frame添加到窗口中。
from tkinter import *
class App:
def __init__(self,master):
self.master = master
self.initWidgets()
def initWidgets(self):
fm1 = Frame(self.master)
fm1.pack(side = LEFT,fill = BOTH,expand = YES)
Button(fm1,text = '第1个').pack(side = TOP,fill = X,expand = YES)
Button(fm1,text = '第2个').pack(side = TOP,fill = X,expand = YES)
Button(fm1,text = '第3个').pack(side = TOP,fill = X,expand = YES)
fm2 = Frame(self.master)
fm2.pack(side = LEFT,padx = 10,expand = YES)
Button(fm2,text = '第1个').pack(side = RIGHT,fill = Y,expand = YES)
Button(fm2,text = '第2个').pack(side = RIGHT,fill = Y,expand = YES)
Button(fm2,text = '第3个').pack(side = RIGHT,fill = Y,expand = YES)
fm3 = Frame(self.master)
fm3.pack(side = RIGHT,padx = 10,fill =BOTH,expand = YES)
Button(fm3,text = '第1个').pack(side = BOTTOM,fill = Y,expand = YES)
Button(fm3,text = '第2个').pack(side = BOTTOM,fill = Y,expand = YES)
Button(fm3,text = '第3个').pack(side = BOTTOM,fill = Y,expand = YES)
root = Tk()
root.title("pack布局")
display = App(root)
root.mainloop()
11.3.2Grid布局管理器;网格布局;
grid()方法;
#grid布局;计算器界面
from tkinter import *
class App:
def __init__(self,master):
self.master = master
self.initWidgets()
def initWidgets(self):
e = Entry(relief = SUNKEN,font = ('courier new',24),width = 25)
e.pack(side = TOP,pady = 10)
p = Frame(self.master)
p.pack(side = TOP)
names = (
'0','1','2','3',
'4','5','6','7',
'8','9','+','-',
'*','/','.','=')
# for i in range(len(names)):
for i in range(len(names)):
# print(i)
b = Button(p,text = names[i],font = ('Verdana',20),width = 6)
b.grid(row = i // 4,column = i % 4)
root = Tk()
root.title("grid布局;计算器界面")
App(root)
root.mainloop()
11.3.3palce布局管理器;绝对布局;
源代码:
#place绝对布局
from tkinter import *
import random
class App:
def __init__(self,master):
self.master = master
self.initWidgets()
def initWidgets(self):
books = (1,2,3,4,5)
for i in range(len(books)):
ct = [random.randrange(256) for x in range(3)]
grayness = int(round(0.299*ct[0] + 0.587*ct[1] + 0.114*ct[2]))
bg_color = '#%02x%02x%02x' %tuple(ct)
lb = Label(
text = books[i],
fg = 'White' if grayness < 125 else 'Black',
bg = bg_color
)
lb.place(x = 20,y = 36 + i*36,width = 180,height = 30)
root = Tk()
root.title("place绝对布局")
root.geometry('250x250+30+30')
App(root)
root.mainloop()
简化代码;去掉随机的部分;
#place绝对布局
from tkinter import *
import random
class App:
def __init__(self,master):
self.master = master
self.initWidgets()
def initWidgets(self):
books = (1,2,3,4,5)
for i in range(len(books)):
lb = Label(
text = books[i],
fg = 'White',
bg = 'black'
)
lb.place(x = 20,y = 36 + i*36,width = 180,height = 30)
root = Tk()
root.title("place绝对布局")
root.geometry('250x250+30+30')
App(root)
root.mainloop()
11.4.1简单的事件处理
#简单的事件处理
from tkinter import *
class App:
def __init__(self,master):
self.master = master
self.initWidgets()
def initWidgets(self):
self.label = Label(self.master,width = 30)
self.label['font'] = ('Courier',20)
self.label['bg'] = 'purple'
self.label.pack()
bn = Button(self.master,text = '单击',command = self.change)
bn.pack()
#定义事件处理方法
def change(self):
self.label['text'] = 'python'
self.label['bg'] = 'blue'
self.label['fg'] = 'yellow'
root = Tk()
root.title("标题")
App(root)
root.mainloop()
点击按钮之后
11.4.2事件绑定;单击/双击
#事件绑定;单击/双击
from tkinter import *
import sys
class App:
def __init__(self,master):
self.master = master
self.initWidgets()
def initWidgets(self):
self.show = Label(self.master,width = 30,bg = 'white',font = ('times',20))
self.show.pack()
bn =Button(self.master,text ='单击/双击')
bn.pack(fill = BOTH,expand = YES)
bn.bind('<Button-1>',self.one_click)
bn.bind('<Double-1>',self.double_click)
def one_click(self,event):
self.show['text'] = '左键单击:%s' % event.widget['text']
def double_click(self,event):
self.show['text'] = '左键双击:%s' % event.widget['text']
sys.exit()
root = Tk()
root.title("事件绑定;单击/双击")
App(root)
root.mainloop()
鼠标移动事件处理
#鼠标移动事件处理
from tkinter import *
class App:
def __init__(self,master):
self.master = master
self.initWidgets()
def initWidgets(self):
lb = Label(self.master,width = 40,height = 3)
lb.config(bg = 'lightgreen',font = ('Times',20))
lb.bind('<Motion>',self.motion)
lb.bind('<B1-Motion>',self.press_motion)
lb.pack()
self.show = Label(self.master,width = 38,height = 1)
self.show.config(bg = 'white',font = ('Courier New',20))
self.show.pack()
def motion(self,event):
self.show['text'] = '鼠标移动(%s %s)' % (event.x,event.y)
return
def press_motion(self,event):
self.show['text'] = '按住鼠标移动(%s %s)' % (event.x,event.y)
return
root = Tk()
root.title('鼠标事件')
App(root)
root.mainloop()
生成计算机;带有单击响应事件;不太理解啊;
#grid布局;计算器;事件响应;
from tkinter import *
class App:
def __init__(self,master):
self.master = master
self.initWidgets()
self.expr = None
def initWidgets(self):
self.show = Label(
relief = SUNKEN,
font = ('Courier New',24),
width = 25,
bg = 'white',
anchor = E
)
self.show.pack(side = TOP,pady = 10)
p = Frame(self.master)
p.pack(side = TOP)
names = (
'0','1','2','3',
'4','5','6','7',
'8','9','+','-',
'*','/','.','=')
for i in range(len(names)):
b = Button(p,text = names[i],font = ('Verdana',20),width = 6)
b.grid(row = i // 4,column = i % 4)
b.bind('<Button-1>',self.click)
if b['text'] == '=':
b.bind('<Double-1>',self.clean)
def click(self,event):
if(event.widget['text'] in ('0','1','2','3','4','5','6','7','8','9','.')):
self.show['text'] = self.show['text']+event.widget['text']
elif(event.widget['text'] in ('+','-','*','/')):
if self.expr is None:
self.expr = self.show['text'] + event.widget['text']
else:
self.expr = self.show['text'] + event.widget['text']
self.show['text'] = ''
elif(event.widget['text'] == '=' and self.expr is not None):
self.expr = self.expr +self.show['text']
print(self.expr)
self.show['text'] = str(eval(self.expr))
self.expr = None
def clean(self,event):
self.expr = None
self.show['text'] = ''
root = Tk()
root.title("计算器")
App(root)
root.mainloop()
11.5Tkinter常用组件
#ttk组件
from tkinter import *
from tkinter import ttk
class App:
def __init__(self,master):
self.master = master
self.initWidgets()
def initWidgets(self):
cb = ttk.Combobox(self.master,font = 24)
cb = Listbox(self.master,font = 24)
for i in ('python','swift','kotlin'):
cb.insert(END,i)
cb.pack(side = RIGHT,fill = BOTH,expand = YES)
f = Frame(self.master)
f.pack(side = RIGHT,fill = BOTH,expand = YES)
lab = Label(self.master,text = '标签',font = 24)
lab.pack(side = TOP,fill = BOTH,expand = YES)
bn = Button(self.master,text = '按钮')
bn.pack()
root = Tk()
root.title("事件处理")
App(root)
root.mainloop()
11.5.2Variable类;组件与变量双向绑定;
#Entry组件与StringVar双向绑定
#可以通过StringVar改变Entry输入框显示的内容
#可以通过StringVar获得Entry输入框中的内容
from tkinter import *
from tkinter import ttk
import random
from tkinter import messagebox
class App:
def __init__(self,master):
self.master = master
self.initWidgets()
def initWidgets(self):
self.st = StringVar()
ttk.Entry(self.master,textvariable = self.st,
width = 24,
font = ('StSong',20,'bold'),
foreground = 'red').pack(fill = BOTH,expand = YES)
f = Frame(self.master)
f.pack()
ttk.Button(f,text = '改变',command = self.change).pack(side = LEFT)
ttk.Button(f,text = '获取',command = self.get).pack(side = LEFT)
def change(self):
books = ('1','2','3')
self.st.set(books[random.randint(0,2)])
def get(self):
messagebox.showinfo(title = '输入内容',message = self.st.get())
root = Tk()
root.title("Variable测试")
App(root)
root.mainloop()