首页
学习
活动
专区
工具
TVP
发布

Tkinter小部件用法介绍

前些时候,小编给大家简单地分享了Tkinter内置模块中小部件的创建方式。今天,我将深入地给大家分享Tkinter中各个小部件的用法。

一、Tkinter部件参数说明

上次,《设计一个界面,很简单》文章里面说明了:Tkinter模块只是Tcl/Tk的包装器。因此,我们创建的小部件参数都可以从Tcl/Tk文档中查阅,其链接如下:http://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm。当然,你也可以从Python官方文档:《Graphical User Interfaces with Tk》查阅各个小部件的参数信息,其链接如下:https://docs.python.org/3.5/library/tk.html。

二、创建Tkinter部件

首先,我们使用以下三行代码创建一个顶级窗体,如下所示:

import tkinter as tk

root = tk.Tk()

root.mainloop()

如果你想要在顶级窗体上添加一个标题,只需要添加一行代码,如下所示:

root.title("Tkinter Widget")

以上代码显示界面如下:

下面创建小部件的内容将省略以上四行代码。

2.1 标签Label

我们将创建一个名为"This is a Label"的标签,其代码如下:

tk.Label(root, text="This is a Label").pack()

如果你希望创建出来的小部件更加美观,请使用以下代码:

from tkinter import ttk

ttk.Label(root, text="This is a Label").pack()

如果你想对Label做更加细微的设置,请参见下面的参数:

activebackground, activeforeground, anchor, background, bitmap, borderwidth, cursor, disabledforeground, font, foreground, highlightbackground, highlightcolor, highlightthickness, image, justify, padx, pady, relief, takefocus, text, textvariable, underline, wraplength, height, state, width。

各参数含义请参见Tcl/Tk官方文档。另外,下面的代码将优先使用ttk里面的小部件。

2.2 按钮Button

我们创建一个名为"This is a Button"的按钮,当点击该按钮时,改变标签的文本颜色及内容,同时修改Button按钮的文字内容。代码如下:

def Change():

label.config(text="This is a Red Label", foreground='red')

button.config(text='Clicked Button')

label = ttk.Label(root, text="A Label")

button = ttk.Button(root, text="This is A Button", command=Change)

label.pack()

button.pack()

点击按钮前的界面:

点击按钮后的界面:

如果你想对Button做更加细微的设置,请参见下面的参数:

activebackground, activeforeground, anchor, background, bitmap, borderwidth, cursor, disabledforeground, font, foreground highlightbackground, highlightcolor, highlightthickness, image, justify, padx, pady, relief, repeatdelay, repeatinterval, takefocus, text, textvariable, underline, wraplength。

各参数含义请参见Tcl/Tk官方文档。

2.3 文本框Entry

我们创建一个文本框,然后输入任意文本,当点击按钮时,输入的文本信息变成按钮的文本信息。如下所示:

def Change():

button.config(text=text.get())

text = tk.StringVar()

ttk.Label(root, text="Enter several text:").grid(column=0, row=0)

textbox = ttk.Entry(root, width=15, textvariable=text)

button = ttk.Button(root, text="Click", command=Change)

textbox.grid(column=0, row=1)

button.grid(column=1, row=1)

点击按钮前的界面:

点击按钮后的界面:

如果你想对Entry做更加细微的设置,请参见下面的参数:

background, bd, bg, borderwidth, cursor, exportselection, fg, font, foreground, highlightbackground, highlightcolor, highlightthickness, insertbackground,

insertborderwidth, insertofftime, insertontime, insertwidth, invalidcommand, invcmd, justify, relief, selectbackground, selectborderwidth, selectforeground, show, state, takefocus, textvariable, validate, validatecommand, vcmd, width,

xscrollcommand.

各参数含义请参见Tcl/Tk官方文档。

2.4 组合框Combobox

我们分别创建一个文本框和组合框,输入任意文本,然后输入数字或者选择下拉选项的数字,选择界面时,光标聚焦在文本框,当单击按钮后,输入的信息成为按钮的文本信息,同时,禁用按钮的显示状态。

def Change():

button.config(text=text.get() + number.get() , state="disabled")

text = tk.StringVar()

number = tk.StringVar()

ttk.Label(root, text="Enter several text:").grid(column=0, row=0)

ttk.Label(root, text="Choose a number:").grid(column=1, row=0)

textbox = ttk.Entry(root, width=15, textvariable=text)

button = ttk.Button(root, text="Click", command=Change)

combobox = ttk.Combobox(root, width=15, textvariable=number)

textbox.focus()

textbox.grid(column=0, row=1)

combobox["values"] = (1, 2, 4, 8, 16, 32)

combobox.current(0)

combobox.grid(column=1, row=1)

button.grid(column=2, row=1)

root.mainloop()

运行程序并选择界面:

输入文本并选择数字后的界面:

如果你想对Combobox做更加细微的设置,请参见下面的参数:

class, cursor, style, takefocus, exportselection, justify, height, postcommand, state, textvariable, values, width。

各参数含义请参见Tcl/Tk官方文档。

2.5 复选框Checkbutton

接着上面组合框的示例,我们将创建三种不同状态的复选框:

禁止选择复选框,默认勾选

可以选择复选框,默认不勾选

可以选择复选框,默认勾选

在上面代码的基础上,增加以下代码创建出下图的复选框:

chk_dis = tk.IntVar()

chk_des = tk.IntVar()

chk_sel = tk.IntVar()

chk1 = tk.Checkbutton(root, text="Disabled", variable=chk_dis, state="disabled")

chk2 = tk.Checkbutton(root, text="Deselect", variable=chk_des)

chk3 = tk.Checkbutton(root, text="Select", variable=chk_sel)

chk1.select()

chk2.deselect()

chk3.select()

chk1.grid(column=0, row=4, sticky=tk.W)

chk2.grid(column=1, row=4, sticky=tk.W)

chk3.grid(column=2, row=4, sticky=tk.W)

如果你想对Checkbutton做更加细微的设置,请参见下面的参数:

activebackground, activeforeground, anchor, background, bd, bg, bitmap, borderwidth, command, cursor, disabledforeground, fg, font, foreground, height, highlightbackground, highlightcolor, highlightthickness, image, indicatoron, justify, offvalue, onvalue, padx,

pady, relief, selectcolor, selectimage, state, takefocus, text, textvariable, underline, varable, width, wraplength.

各参数含义请参见Tcl/Tk官方文档。

2.6 单选按钮Radiobutton

接着上面复选框的示例,我们将创建三个单选按钮,当选择任意一个单选按钮时,将主窗体背景空隙填充不同的颜色。

在上面代码的基础上,增加以下代码创建出下图的单选按钮:

def Colorselector():

color = rad.get()

if color == 1:

root.config(background="RED")

elif color == 2:

root.config(background="GREEN")

elif color ==3:

root.config(background="BLUE")

rad = tk.IntVar()

rad1 = tk.Radiobutton(root, text="RED", variable=rad, value=1, command=Colorselector)

rad2 = tk.Radiobutton(root, text="GREEN", variable=rad, value=2, command=Colorselector)

rad3 = tk.Radiobutton(root, text="BLUE", variable=rad, value=3, command=Colorselector)

rad1.grid(column=0, row=5, sticky=tk.W)

rad2.grid(column=1, row=5, sticky=tk.W)

rad3.grid(column=2, row=5, sticky=tk.W)

运行时的界面:

选择第一个单选按钮的界面:

如果你想对Radiobutton做更加细微的设置,请参见下面的参数:

activebackground, activeforeground, anchor, background, bd, bg, bitmap, borderwidth, command, cursor, disabledforeground, fg, font, foreground, height, highlightbackground, highlightcolor, highlightthickness, image, indicatoron, justify, padx, pady, relief, selectcolor, selectimage, state, takefocus, text, textvariable, underline, value, variable, width, wraplength.

各参数含义请参见Tcl/Tk官方文档。

以上内容简单地介绍了Tkinter模块中主要小部件的用法,希望对大家有所帮助。

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

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券