前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python进阶-GUI-控件

Python进阶-GUI-控件

作者头像
小团子
发布2019-07-18 15:30:29
9000
发布2019-07-18 15:30:29
举报
文章被收录于专栏:数据云团数据云团

让 Packer 来管理和显示控件,最后调用 mainloop() 运行 GUI 应用。

代码语言:javascript
复制
>>> import tkinter
>>> top = tkinter.Tk()
>>> label = tkinter.Label(top, text="数据云团")
>>> label.pack()
>>> tkinter.mainloop()
  • Button 控件

创建按钮控件

代码语言:javascript
复制
import tkinter

top = tkinter.Tk()

quit = tkinter.Button(top, text='退出', command=top.quit)
quit.pack()
tkinter.mainloop()

该按钮有一个额外的参数,tkinter.quit() 方法。该参数会给按钮安装一个回调函数,当按钮被按下(并释放后),整个程序就会退出。

  • Label 和 Button 控件

fill 参数告诉 Packer 让 QUIT 按钮占据剩余的水平空间,而 expand 参数则会引导它填充整个水平可视空间,将按钮拉伸到左右窗口边缘。

代码语言:javascript
复制
import tkinter

top = tkinter.Tk()
text = tkinter.Label(top, text="数据云团")
text.pack()

quit = tkinter.Button(top, text="加鸡腿", command=top.quit, bg="red", fg="white")
quit.pack(fill=tkinter.X, expand=1)

tkinter.mainloop()
  • Label、Button 和 Scale 控件

Scale 滑块是用来控制 Label 控件中文字字体大小的工具。滑块的位置值越大,字体越大。

Scale 用于与 Label 控件进行交互。

  • resize() 回调函数,该函数依附于 Scale 控件。当 Scale 控件的滑块移动时,这个函数就会被激活,用来调整 Label 控件中的文字大小。
  • 定义顶层窗口的大小为 250*150
  • 应用启动时滑块的初始值设定为 12
代码语言:javascript
复制
import tkinter

def resize(ev=None):
  label.config(font="Helvetica -%d bold" % scale.get())
  
top = tkinter.Tk()
top.geometry('250x150')

label = tkinter.Label(top, text="数据云团", font="Helvetica -12 bold")
label.pack(fill=tkinter.Y, expand=1)

scale = tkinter.Scale(top, from_=10, to=40, orient=tkinter.HORIZONTAL, command=resize)
scale.set(12)
scale.pack(fill=tkinter.X, expand=1)

quit = tkinter.Button(top, text="点击", command=top.quit, activeforeground="white", activebackground="red")
quit.pack()
tkinter.mainloop()
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-04-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据云团 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档