当前位置:网站首页>Tkinter学习笔记(二)
Tkinter学习笔记(二)
2022-06-11 21:52:00 【CHRN晨】
一:读取、改变屏幕和窗口信息
#获取屏幕和窗口信息
import tkinter as tk
root=tk.Tk()
#获取屏幕宽度和高度
screenwidth=root.winfo_screenwidth()
screenheight=root.winfo_screenheight()
#获取窗口宽度和高度
winwidth=root.winfo_width()
winheight=root.winfo_height()
#获取窗口的x,y坐标
x=root.winfo_x()
y=root.winfo_y()
#设置窗口:改变窗口的标题,大小,位置,状态
#改变标题
root.title("窗口标题")
#改变窗口长宽和位置
width=300
height=200
x=150
y=150
root.geometry('{}x{}+{}+{}'.format(width,height,x,y))
#设置窗口的新图标
#root.iconbitmap('tt.ico')
#设置最顶层窗口
root.attributes('-topmost',1)
#窗口的最大最小化
#root.state('zoomed')#窗口最大化
#设置为全屏窗口
#root.attributes("-fullscreen",True)
#创建子窗口,子窗口处理为tkinter系统的控制权外,拥有的属性和TK各个窗口是一样的
top=tk.Toplevel(root)
root.mainloop()
二:控件的几种布局方法
#控件的几何布局方法
#pack()布局,默认先使用的放到上面,然后依次往下排.
'''import tkinter as tk root=tk.Tk() root.title("pack()布局") root.geometry('100x100') tk.Label(root,text='A',bg='red').pack(side=tk.LEFT,expand=1) tk.Label(root,text='B',bg='green').pack(side=tk.TOP,expand=1) tk.Label(root,text='V',bg='yellow').pack(side=tk.RIGHT,expand=1) tk.Label(root,text='D',bg='blue').pack(side=tk.BOTTOM,expand=1) root.mainloop()'''
#grid()布局是使用行列位置IDE坐标来放置控件的集合布局管理器
#注意:grid()布局和pack()布局不能同时使用
'''import tkinter as tk root=tk.Tk() root.title("grid()布局") root.geometry('150x150') tk.Label(root,text='A',bg='red').grid(row=0,column=0) tk.Label(root,text='B',bg='green').grid(row=0,column=1) tk.Label(root,text='V',bg='yellow').grid(row=2,column=3,columnspan=2) tk.Label(root,text='D',bg='blue').grid(row=3,column=4,rowspan=2) root.mainloop()'''
#place()布局,显示指定控件的绝对位置或相对于其他控件的位置
import tkinter as tk
root=tk.Tk()
root.title("place()布局")
root.geometry('200x200')
tk.Label(root,text='A',bg='red').place(x=10,y=20)
tk.Label(root,text='B',bg='green').place(x=20,y=30)
tk.Label(root,text='V',bg='yellow').place(x=30,y=40)
tk.Label(root,text='D',bg='blue').place(x=40,y=50)
root.mainloop()



三:创建窗口菜单条
#创建窗口菜单条
import tkinter as tk
root=tk.Tk()
def hello():
print('hello')
root.title('主窗口标题')
root.geometry('300x300')
#创建主菜单
MainMenu=tk.Menu(root)
#创建子菜单
filemenu=tk.Menu(MainMenu,tearoff=False)
#trearoff为false表示不会显示菜单线
helpmenu=tk.Menu(MainMenu,tearoff=True)
#把子菜单加入到主菜单中
MainMenu.add_cascade(label="菜单",menu=filemenu)
MainMenu.add_cascade(label="帮助",menu=helpmenu)
#建立子菜单项目
filemenu.add_command(label="读取",command=hello)
filemenu.add_command(label="保存",command=hello)
filemenu.add_separator()#增加分割线
filemenu.add_command(label='另存为',command=hello)
filemenu.add_command(label='查看',command=hello)
#给窗口配置菜单
root.config(menu=MainMenu)
root.mainloop()

四:创建弹出窗口菜单
#与主菜单的创建过程类似,只是配置菜单方式不同
import tkinter as tk
#将窗口移动到屏幕中间
def setCenter(root,w,h):
ws=root.winfo_screenwidth()
hs=root.winfo_screenheight()
x=int((ws/2)-(w/2))
y=int((hs/2)-(h/2))
root.geometry('{}x{}+{}+{}'.format(w,h,x,y))
def hello():
print("hello")
count=0
def update():
global count
count+=1
menu.entryconfig(0,label=str(count)+"运行次数")
root=tk.Tk()
root.title('窗口内容')
menu=tk.Menu(root,tearoff=True)#创建弹出菜单
filemenu=tk.Menu(menu,tearoff=False)
#创建子菜单项目
menu.add_command(label='Undo',command=hello)
menu.add_command(label='Redo',command=hello)
menu.add_command(label='Copy',command=hello)
menu.add_command(label='Paste',command=hello)
menu.add_command(label='Cut',command=hello)
menu.add_separator()
menu.add_cascade(label='菜单',menu=filemenu)
menu.add_command(label='SelectAll',command=hello)
filemenu.add_command(label='读取',command=hello)
filemenu.add_command(label='保存',command=hello)
filemenu.add_separator()
filemenu.add_command(label='另存',command=hello)
filemenu.add_command(label='查看',command=hello)
#create a canvas
frame=tk.Frame(root,width=534,height=534)
frame.pack()
#弹出菜=菜单操作函数
def popup(event):
menu.post(event.x_root,event.y_root)
#把弹出菜单绑定到所需的控件上
frame.bind("<Button-3>",popup)
root.mainloop()

边栏推荐
猜你喜欢

R语言书籍学习03 《深入浅出R语言数据分析》-第十章 关联规则 第十一章 随机森林

「大模型」之所短,「知识图谱」之所长

The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace

The shortcomings of the "big model" and the strengths of the "knowledge map"

Flink error: multiple tasks are started, and only one task is executed

In the post epidemic era, how can enterprise CIOs improve enterprise production efficiency through distance

华为设备配置H-VPN

MySQL事务简介

学习位段(1)

Leetcode - day 2
随机推荐
超标量处理器设计 姚永斌 第2章 Cache --2.2 小节摘录
In the post epidemic era, how can enterprise CIOs improve enterprise production efficiency through distance
效率起飞啊!还能这样开发的?
Introduction à endnotex9 et instructions pour les tutoriels de base
crontab中定时执行shell脚本
go encoding包
常用分页方法总结
每日一题 - 罗马数字转整数
Go OS module
Popular science | what are the types of NFT (Part 1)
[niuke.com] ky41 put apples
【LeetCode】11. Container with the most water
Precision twist jitter
动态内存管理(1)
Parker plunger pump pv180r1k1t1nmmc
Win10弹出USB时出现该设备正在使用的解决方法
C language implements eight sorts (3)
自定义实现offsetof
[today in history] June 11: the co inventor of Monte Carlo method was born; Google launched Google Earth; Google acquires waze
How does the chief financial officer of RPA find the "super entrance" of digital transformation?