本文主要是介绍python 窗口,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、界面
1、创建窗口
import tkinter
#主窗口
win = tkinter.Tk()
#窗口标题
win.title("hello word")
#设置位置,宽,高,距离左方,距离上方
win.geometry("400x100+200+100")
#显示窗口
win.mainloop()
2、标签控件label
#显示文本控件label
label = tkinter.Label(win,
text = "good morning",#文本内容
bg = "pink",#文本背景颜色
fg = "red",#字体颜色
font = ("宋体",20),#什么字体,字体大小
height = 10,#文本内容的高
width = 20,#文本内容的宽
wraplength = 100,#文本中多宽进行换行
justify = "left",#换行之后的对齐方式
anchor = "center"#文本位置,n、e、s、w、ne、es、sw、wn
)
#将控件显示出来
label.pack()
3、按钮
def func():
print("hello")
button = tkinter.Button(win,
text="按钮", #按钮上的字
command=func,#点击按钮执行函数,可以执行匿名函数
width = 10,#宽
height = 20#高
)
button.pack()
4、单行文本框
e = tkinter.Variable()
entry = tkinter.Entry(win,#主窗口
#show="*",#文本框中的内容以*替代,相当于密码框
textvariable=e#绑定变量,e就是entry对象
)
entry.pack()
#文本框设置值
e.set("hello word")
#取出文本框内的内容
print(e.get())
print(entry.get())
5、多行文本框
#滚动条
scroll =tkinter.Scrollbar()
#多行文本框,width几列,height几行
text = tkinter.Text(win,
width=50,
height = 8
)
#side放在窗口的哪一侧,fill填充
scroll.pack(side = tkinter.RIGHT, fill=tkinter.Y)
text.pack(side = tkinter.LEFT, fill = tkinter.Y)
#关联
scroll.config(command=text.yview)
text.config(yscrollcommand=scroll.set)
str = '''
The cynics say the college students are the pampered lost generation, which would cringe at the slightest discomfort. But the cynics are wrong. The college students I see are eagerly learning about how to live independently. We help each other clean the dormitory, go shopping and bargain together, and take part time jobs to supplement our pocket money.
Recently, ther is a heated debate in our society. The college students are the beneficiaries of a rare privilege, who receive exceptional education at extraordinary places. But will we be able to face the challenge and support ourselves against all odds? Will we be able to better the lives of others? Will we be able to accept the responsibility of building the future of our country?
'''
text.insert(tkinter.INSERT,str)
6、复选框
def updata():
message = ""
if hobby1.get() == True:
message += "tom\n"
if hobby2.get() == True:
message += "jim\n"
if hobby3.get() == True:
message += "lily\n"
#删除文本框内容
text.delete(0.0,tkinter.END)
#将message添加到文本框中
text.insert(tkinter.INSERT,message)
hobby1 = tkinter.BooleanVar()
hobby2 = tkinter.BooleanVar()
hobby3 = tkinter.BooleanVar()
#创建复选框
check1 = tkinter.Checkbutton(win,#主窗口
text="tom",#多选框标题
variable=hobby1,#绑定变量
command=updata#函数
)
#显示复选框
check1.pack()
check2 = tkinter.Checkbutton(win,
text="jim",
variable=hobby2,
command=updata
)
#显示复选框
check2.pack()
check3 = tkinter.Checkbutton(win,
text="lily",
variable=hobby3,
command=updata
)
#显示复选框
check3.pack()
text = tkinter.Text(win,width=50,height=5)
text.pack()
7、单选框
def updata():
print(r.get())
r =tkinter.IntVar()
radio1 = tkinter.Radiobutton(win,
text="one",#按钮名字
value = 44,#按钮的值,函数打印的就是值,函数打印的就是
variable=r,#绑定变量
command=updata
)
radio1.pack()
radio2 = tkinter.Radiobutton(win,
text="one",
value = 45,
variable=r,
command=updata
)
radio2.pack()
8、列表框
#列表框
lb = tkinter.Listbox(win,
selectmode=tkinter.BROWSE
)
lb.pack()
#往列表框中添加数据
for i in ['good','nice','very good']:
#在结尾添加
lb.insert(tkinter.END,i)
#往列表框中第一行添加数据
lb.insert(tkinter.ACTIVE, 'COOL')
#删除下标索引为2-3行的数据,也可以只填2
#lb.delete(2,3)
#选中下标索引为2-3行的数据,也可以只填2,取消选中为lb.select_clear,用法和选中一样
lb.select_set(2,3)
#获取列表元素个数
#print(lb.size())
#获取列表中的值,第2-3行的值,也可以只填2
#print(lb.get(2,3))
#返回当前的下标索引
print(lb.curselection())
#判断一个选项是否被选中
print(lb.select_includes(2))
9、列表框绑定事件
#绑定变量
lbv = tkinter.StringVar()
#不支持鼠标移动选中选中位置,支持鼠标按下选中位置
lb = tkinter.Listbox(win,
selectmode = tkinter.SINGLE,
listvariable = lbv
)
lb.pack()
for i in ['good','nice','very good']:
#在结尾添加
lb.insert(tkinter.END,i)
#print(lbv.get())
#设置选项
#lbv.set(('1','2','3'))
#绑定事件
def MyPrint(event):
#打印双击选中的值
print(lb.get(lb.curselection()))
#双击选中执行函数MyPrint
lb.bind('<Double-Button-1>',MyPrint)
10、列表框多选(全选)
lb = tkinter.Listbox(win,
selectmode = tkinter.EXTENDED
)
lb.pack()
for i in ['good','nice','very good','zzz','ffff','good','nice','very good','zzz','ffff''good','nice','very good','zzz','ffff''good','nice','very good','zzz','ffff']:
lb.insert(tkinter.END,i)
11、菜单栏
menubar = tkinter.Menu(win)
win.config(menu = menubar)
#创建一个菜单选项
menul = tkinter.Menu(menubar,tearoff = False)
#给菜单选项添加内容
for i in ['C','PHP','C++','JAVA','JS','quit']:
if i == 'quit':
menul.add_command(label=i,command=win.quit)
else:
menul.add_command(label=i)
#向菜单条上添加菜单选项
menubar.add_cascade(label="语言",menu=menul)
12、鼠标右键菜单栏
menubar = tkinter.Menu(win)
menu = tkinter.Menu(menubar,tearoff = False)
for i in ['C','PHP','C++','JAVA','JS','quit']:
menu.add_command(label=i)
menubar.add_cascade(label="语言", menu=menu)
#显示菜单
def showMenu(event):
menubar.post(event.x_root, event.y_root)
#鼠标右键调用函数showMenu显示菜单
win.bind('<Button-3>',showMenu)
13、下拉菜单
from tkinter import ttk
#绑定变量
cv = tkinter.StringVar()
com = ttk.Combobox(win,textvariable=cv)
com.pack()
#下拉菜单的值
com['value'] = ('C','C++','JS')
#设置默认值
com.current(0)
def func(event):
print(cv.get())
#绑定事件
com.bind('<<ComboboxSelected>>',func)
14、窗口布局
# 在主窗口win上布局
frm = tkinter.Frame(win)
# 将页面显示出来
frm.pack()
# 在窗口frm上布局
frm_1 = tkinter.Frame(frm)
# 在窗口的哪个地方
tkinter.Label(frm_1,text = '左上', bg = 'pink').pack(side = tkinter.TOP)
tkinter.Label(frm_1,text = '左下', bg = 'blue').pack(side = tkinter.TOP)
# 将窗口显示出来
frm_1.pack(side = tkinter.LEFT)
frm_2 = tkinter.Frame(frm)
tkinter.Label(frm_2,text = '右上', bg = 'red').pack(side = tkinter.TOP)
tkinter.Label(frm_2,text = '右下', bg = 'grey').pack(side = tkinter.TOP)
frm_2.pack(side = tkinter.RIGHT)
15、表格
from tkinter import ttk
# 表格
tree = ttk.Treeview(win)
tree.pack()
# 定义列,每列取个名字用来标识
tree['columns'] = ('姓名','年龄','身高','体重')
# 设置列的宽度
tree.column('姓名',width=100)
tree.column('年龄',width=100)
tree.column('身高',width=100)
tree.column('体重',width=100)
# 设置列的内容
tree.heading('姓名', text="name")
tree.heading('年龄', text="age")
tree.heading('身高', text="height")
tree.heading('体重', text="weigth")
# 添加数据, 0代表第一行,如果再来一个0会将第一行的挤下去,text为行的内容,values为值
tree.insert('', 0, text = 'line1', values = ('tom', '28', '175', '75'))
tree.insert('', 0, text = 'line1', values = ('jim', '22', '165', '75'))
16、目录结构
from tkinter import ttk
# 表格
tree = ttk.Treeview(win)
tree.pack()
# 最上层目录
treeF1 = tree.insert('', 0, '中国', text='china', values=('F1'))
treeF2 = tree.insert('', 1, '美国', text='american', values=('F2'))
treeF3 = tree.insert('', 2, '英国', text='english', values=('F3'))
# treeF1目录下的子目录
treeF1_1 = tree.insert(treeF1, 0, '黑龙江', text='黑龙江', values=('F1_1'))
treeF1_2 = tree.insert(treeF1, 1, '吉林', text='吉林', values=('F1_2'))
treeF1_3 = tree.insert(treeF1, 2, '武汉', text='武汉', values=('F1_3'))
17、绝对布局
label1 = tkinter.Label(win,text = 'good' , bg = 'blue')
label2 = tkinter.Label(win,text = 'nice' , bg = 'red')
label3 = tkinter.Label(win,text = 'cool' , bg = 'yellow')
label1.place(x = 10 , y = 10)
label2.place(x = 50 , y = 50)
label3.place(x = 100 , y = 100)
18、相对布局
label1 = tkinter.Label(win,text = 'good' , bg = 'blue')
label2 = tkinter.Label(win,text = 'nice' , bg = 'red')
label3 = tkinter.Label(win,text = 'cool' , bg = 'yellow')
label1.pack(fill = tkinter.Y, side = tkinter.LEFT)
label2.pack(fill = tkinter.X, side = tkinter.TOP)
label3.pack(fill = tkinter.Y, side = tkinter.RIGHT)
19、控件点击事件
def func(event):
print(event.x, event.y)
# 鼠标点击事件Button-1,2,3对应左、中、右,双击可以用Double-Button-1
buttonl = tkinter.Button(win, text='leftmouse button')
buttonl.bind('<Button-1>', func)
buttonl.pack()
20、鼠标滑动事件
def func(event):
print(event.x,event.y)
# 鼠标滑动事件B1-Motion,1,2,3
buttonl = tkinter.Button(win, text='leftmouse button')
buttonl.pack()
buttonl.bind('<B1-Motion>',func)
21、鼠标进入离开事件
# <Leave>离开
Label = tkinter.Label(win,text="good", bg ='red')
Label.pack()
def func(event):
print(event.x,event.y)
Label.bind("<Enter>",func)
22、响应所有按键
def func(event):
print(f'event.char = {event.char}')
print(f'event.keycode = {event.keycode}')
Label = tkinter.Label(win,text="good", bg ='red')
# 设置焦点
Label.focus_set()
Label.pack()
Label.bind("<Key>",func)
二、控制窗口
1、下载模块pip install pywin32
2、获取窗口类和标题
3、每2秒显示和隐藏窗口
import win32con, win32gui, time
while 1:
DingDing = win32gui.FindWindow("StandardFrame_DingTalk", "钉钉")
win32gui.ShowWindow(DingDing, win32con.SW_SHOW)
time.sleep(2)
win32gui.ShowWindow(DingDing, win32con.SW_HIDE)
time.sleep(2)
4、窗口随机移动
import win32con, win32gui, time, random
while 1:
DingDing = win32gui.FindWindow("StandardFrame_DingTalk", "钉钉")
x = random.randrange(600)
y = random.randrange(300)
#参数1:控制的窗体,参数2:大致方位,HWND_TOPMOST上方 ,参数3:位置x , 参数4:位置y,参数5:场地,参数6:宽度
win32gui.SetWindowPos(DingDing, win32con.HWND_TOPMOST, x, y, 30, 30, win32con.SWP_SHOWWINDOW)
5、操作内存
这篇关于python 窗口的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!