Tkinter 简介
图形用户界面 (GUI)是一种用户界面形式,它允许用户使用图标、菜单、窗口等项目通过视觉指示器与计算机进行交互。它比用户与计算机交互的命令行界面 (CLI) 具有优势通过仅使用键盘编写命令并且其使用比 GUI 更困难。
什么是 Tkinter?
Tkinter是用于创建 GUI 应用程序的内置Python模块。它是在Python中创建 GUI 应用程序最常用的模块之一,因为它简单易用。您无需担心单独安装 Tkinter 模块,因为它已经随Python提供。它为 Tk GUI 工具包提供了一个面向对象的接口。其他一些可用于创建我们自己的 GUI 应用程序的Python库是
在所有 Tkinter 中使用最广泛
什么是小部件?
Tkinter 中的小部件是 GUI 应用程序的元素,它为用户提供各种控件(如标签、按钮、组合框、复选框、菜单栏、单选按钮等)以与应用程序交互。
tkinter 程序的基本结构基本的 Tkinter 小部件:
Widgets | Description |
---|---|
Label | It is used to display text or image on the screen |
Button | It is used to add buttons to your application |
Canvas | It is used to draw pictures and others layouts like texts, graphics etc. |
ComboBox | It contains a down arrow to select from list of available options |
CheckButton | It displays a number of options to the user as toggle buttons from which user can select any number of options. |
RadiButton | It is used to implement one-of-many selection as it allows only one option to be selected |
Entry | It is used to input single line text entry from user |
Frame | It is used as container to hold and organize the widgets |
Message | It works same as that of label and refers to multi-line and non-editable text |
Scale | It is used to provide a graphical slider which allows to select any value from that scale |
Scrollbar | It is used to scroll down the contents. It provides a slide controller. |
SpinBox | It is allows user to select from given set of values |
Text | It allows user to edit multiline text and format the way it has to be displayed |
Menu | It is used to create all kinds of menu used by an application |
例子
from tkinter import *
from tkinter.ttk import *
# writing code needs to
# create the main window of
# the application creating
# main window object named root
root = Tk()
# giving title to the main window
root.title("First_Program")
# Label is what output will be
# show on the window
label = Label(root, text ="Hello World !").pack()
# calling mainloop method which is used
# when your application is ready to run
# and it tells the code to keep displaying
root.mainloop()
输出
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。