首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python 学习之 Tkinter“上”

阅读文本大概需要 6.6 分钟

最近在跟着学做一个简单的银行系统,做界面要用到 GUI「 图形用户界面编程 」,所以今天先为大家介绍一下相关的知识,很有趣,大家看完后练下手。

百度百科以及查阅的资料介绍:

Tkinter: Tkinter 模块(Tk 接口)是 Python 的标准 Tk GUI 工具包的接口 。Tk 和 Tkinter 可以在大多数的 Unix 平台下使用,同样可以应用在 Windows 和 Macintosh 系统里。Tk8.0 的后续版本可以实现本地窗口风格,并良好地运行。

Python 支持多种图形界面的第三方库,包括:Tk、wxWidgets、Qt、GTK 等等。但是 Python 自带的库是支持 Tk 的 Tkinter ,使用 Tkinter ,无需安装任何包,就可以直接使用。我们编写的 Python 代码会调用内置的 Tkinter,Tkinter 封装了访问 Tk 的接口;Tk 是一个图形库,支持多个操作系统,使用Tcl 语言开发;Tk 会调用操作系统提供的本地 GUI 接口,完成最终的 GUI。

创建一个简单窗口

importtkinter

# 创建主窗口

win = tkinter.Tk()

win.title("Mark")# 标题

win.geometry("400x400+400+200")# 大小和位置

# 进入消息循环,显示窗口界面

win.mainloop()

标签控件

# 该代码块位于 win.mainloop() 之前,先将组件加载到窗口上,再显示窗口界面

label = tkinter.Label(win, text="To be a better man !", bg="blue", fg="white", font=("黑体",20), width=10, height=4, wraplength=50, justify="left", anchor="center")

label.pack()# 加载、显示标签

按钮控件

deff():

print("To be a better man !")

# 绑定事件,点击按钮控制台输出语句

btn1 = tkinter.Button(win, text="点击1", command=f, width=10, height=2)

btn1.pack()

# 匿名函数输出打印语句

btn2 = tkinter.Button(win, text="点击2", command=lambda: print("To make right decisions !"))

btn2.pack()

# 退出窗口

btn3 = tkinter.Button(win, text="退出", command=win.quit)

btn3.pack()

输入控件 I

# 输入控件,用于显示简单的文本内容

entry = tkinter.Entry(win, show="*")# show="*" 密文显示

entry.pack()

输入控件 II

# 为控件绑定变量

v = tkinter.Variable()

entry = tkinter.Entry(win, textvariable=v)

entry.pack()

# v 代表输入框这个对象,设置默认值

v.set("To be a better man !")

# 获取输入控件的值,输出在控制台

print(v.get())

print(entry.get())

例:点击按钮输出输入框的内容

defshowInfo():

print(entry.get())

entry = tkinter.Entry(win)

entry.pack()

btn = tkinter.Button(win, text="点击", command=showInfo)

btn.pack()

带滚动条的 Text 控件

t = tkinter.Text(win, width=60, height=10)

sc = tkinter.Scrollbar()# 滚动条

# side 控件放置在窗体的那一侧,fill 填充方向

sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)

t.pack(side=tkinter.LEFT, fill=tkinter.Y)

# 关联文本框和滚动条

sc.config(command=t.yview)

t.config(yscrollcommand=sc.set)

s ='''Everyone has his own understanding of young,it is a period of time of

beauty and wonders,only after you have experienced the sour ,sweet ,

bitter and salty can you really become a person of significance.there

time of young is limited,it may pass by without your attention,and

when you discover what has happened ,it is always too late.grasping

the young well means a better time is waiting for you in the near future,

or the situation may be opposite .Having a view on these great men in the history

of human being,they all made full use of their youth time ,to do things that are

useful to society,to the whole mankind,and as a consequence ,they are remembered by

later '''

t.insert(tkinter.INSERT, s)

复选框 Checkbutton

defupdate():

message =""

ifh1.get()isTrue:

message +="Money\n"

ifh2.get()isTrue:

message +="Power\n"

ifh3.get()isTrue:

message +="Position\n"

# 清除 text 中的所有内容

t.delete(0.0, tkinter.END)

# 复选框选中后,将其对应内容写到文本框中

t.insert(tkinter.INSERT, message)

h1 = tkinter.BooleanVar()

c1 = tkinter.Checkbutton(win, text="Money", variable=h1, command=update)

c1.pack()

h2 = tkinter.BooleanVar()

c2 = tkinter.Checkbutton(win, text="Power", variable=h2, command=update)

c2.pack()

h3 = tkinter.BooleanVar()

c3 = tkinter.Checkbutton(win, text="Position", variable=h3, command=update)

c3.pack()

t = tkinter.Text(win, width=60, height=6)

t.pack()

单选框

defupdate():

print(v.get())

# 注意:一组单选框要绑定同一个变量

v = tkinter.IntVar()

r1 = tkinter.Radiobutton(win, text="apple", value=1, variable=v, command=update)

r1.pack()

r2 = tkinter.Radiobutton(win, text="orange", value=2, variable=v, command=update)

r2.pack()

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180627G14I2R00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券