赞
踩
目录
tkinter是使用python进行窗口视窗设计的模块,它是python的标准Tk GUI工具包接口,python默认安装了这个模块。
(python3 中,该模块更名为tkiner)
widget组件的分类
类型 | 包含组件 |
文本类 | Label:标签 Entry:单行文本组件 Text:多行文本组件 Spinbox::输入组件,列表菜单和单行文本框的组合体 Scale: 数字范围组件 |
按钮类 | Button Radiobutton:单选 Checkbutton: 多选 |
选择列表类组件 | ListBox:列表框 Scrollbar:滚动条 OptionMenu:下拉列表 Combobox:组合框 |
容器类 | Frame:框架组件 LableFrame:标签框架组件 Toplevel:顶层窗口 PaneWindow:窗口布局管理 Notebook:选项卡组件 |
会话类 | Message MessageBox |
菜单类 | Menu Toolbar Treeview |
进度条类 | ProgressBar |
widget的公共属性:
文字样式的一些常用属性:
Demo:
- Label(win
- ,text="This is a Test"
- ,fg="red"
- ,bg="black"
- ,width=40
- ,height=40
- ,anchor="nw"
- ,font="华文新魏 14 bold"
- ).pack(padx=20,pady=10)
widget的公共方法:
- config(): 为组件配置参数
- keys(): 获取组件的所有参数,并返回一个列表
- lable=Lable(win,text="Exe")
- lable.config(bg="red",fg="white")
ttk是tkinter中一个很重要的模块,相当于它的升级版,虽然tkinter中包含了很多模块,但是这些组件样式比较简单,然后就出现了ttk模块。
ttk包含了18个组件,(其中12个组件在tkinter中已经包含),特殊的几个为Combobox,Notebook,Progressbar,Separator,Sizegrip、TreeView。
嗯,tkinter使用的是windows的主题风格;ttk模块使用的是windows默认主题的风格。
导入:
from tkinter.ttk import *
如果希望用ttk模块中的组件样式覆盖tkinter的,可以用这样的方式:
- from tkinter import *
- from tkinter.ttk import *
Demo1:
- from tkinter import *
-
- win = Tk()
- win.title('Name This Window')
-
- btn=Button(win,text="Hello",font=14,relief="flat",bg="#00ff12").pack(pady=20)
-
- win.mainloop()
Demo2:
- from tkinter import *
- from tkinter import ttk
- from tkinter.ttk import *
-
- root = Tk()
- root.title('Name This Window')
- style=Style() # 创建style对象,用于设置样式。
- style.configure("TButton",font=12,relief="flat",background="#FF8933")
- btn=ttk.Button(text="this is a Test",style="TButton").pack(pady=20)
-
- root.mainloop()
tkinter的窗口,也被称为容器。创建窗口需要实例化Tk()方法,然后通过mainloop方法让程序进入等待与处理窗口事件,直到窗口关闭。
Demo1:
- from tkinter import *
-
- root = Tk()
- root.title('Name This Window') # 设置标题
- root.geometry("900x500") # 设置窗口大小
- root.maxsize(width=1200,height=700) # 最大尺寸
- root.config(bg="#FF9435") # 设置背景色
- # root.resizable(False,False)
- # root.iconbitmap
-
- text=Label(root,text="This is a Demo").pack()
- root.mainloop()
Demo2:
- from tkinter import *
- win=Tk()
- win.title("Test")
- win.configure(bg="#FFea90")
- width,height=300,100
- scrw,scrh=win.winfo_screenwidth(),win.winfo_screenheight()
- x=(scrw-width)/2
- y=(scrh-height)/2
- win.geometry("%dx%d+%d+%d" % (width,height,x,y)) # 设置窗口的位置 (x和y,已右上角为原点的坐标系)
-
- win.mainloop()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。