#滚动条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 atextraordinary 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)
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'))#绑定事件defMyPrint(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)
#显示菜单defshowMenu(event):
menubar.post(event.x_root, event.y_root)
#鼠标右键调用函数showMenu显示菜单
win.bind('<Button-3>',showMenu)
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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?