热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用tkinter模块在Python中进行GUI编程

GUI(GraphicalUserInterface):GUI(图形用户界面):GUIisasimpleapplicationwhichhelpstheusertoi

GUI (Graphical User Interface):

GUI(图形用户界面):

GUI is a simple application which helps the user to interact with the computer or any other electronic device through a graphical icon. This used to perform different tasks on a desktop or laptop.

GUI是一个简单的应用程序,可以帮助用户通过图形图标与计算机或任何其他电子设备进行交互。 这通常用于在台式机或笔记本电脑上执行不同的任务。

GUI tkinter模块 (GUI tkinter module)

tkinter is an inbuilt Python module used to create a GUI application. Python offers a lot of options for creating GUI out of which tkinter is most commonly used. You don't need to worry about installation because it comes with Python.

tkinter是用于创建GUI应用程序的内置Python模块。 Python提供了许多创建tkinter的 GUI的选项。 您无需担心安装,因为它是Python附带的。

There is the most common way to create a GUI application using tkinter:

使用tkinter创建GUI应用程序的最常用方法:

  • Import the tkinter module in the program.

    将tkinter模块导入程序中。

  • Create the GUI application's main window.

    创建GUI应用程序的主窗口

  • Add any number of widgets to the GUI application's main window.

    将任意数量的小部件添加到GUI应用程序的主窗口。

  • Apply the main event loop to widgets.

    将主事件循环应用于小部件

There are mainly two methods we have to remember during the creation of the GUI application by using tkinter module in Python.

使用Python中的tkinter模块创建GUI应用程序的过程中,我们主要需要记住两种方法。

1) tkinter.Tk()

1)tkinter.Tk()

To create the main window of the GUI application tkinter offers a Tk() function.

为了创建GUI应用程序的主窗口,tkinter提供了Tk()函数。

Syntax:

句法:

Includehelp=tkinter.Tk()

Where, Includehelp is the name of the GUI application's main window.

其中, Includehelp是GUI应用程序主窗口的名称。

2) mainloop()

2)mainloop()

This is used when you are ready for the application to run. This telling the code keep displaying the window until manually closed it.

当您准备好运行应用程序时,将使用它。 这告诉代码继续显示该窗口,直到手动将其关闭为止。

Syntax:

句法:

Includehelp.mainloop()

In tkinter, all widgets will have some geometry measurement and it has three mainly geometry manager classes which are discussed below.

tkinter中 ,所有小部件都将具有一些几何尺寸,并且它具有以下三个主要的几何管理器类。

  1. pack(): It organizes the widgets in blocks before placing them in the parent widget.

    pack() :在将小部件放置在父小部件中之前,将它们按块进行组织。

  2. grid(): It organizes the widgets in the grid before placing it in the parent widget.

    grid() :在将其放置在父窗口小部件中之前,它会组织网格中的窗口小部件。

  3. place(): It organizes the widgets by placing them on specific positions directed by us.

    place() :通过将小部件放在我们指示的特定位置来组织小部件。

In tkinter, there is a lot of widgets provided to use in the GUI application. Some of these major widgets will be discussed below:

tkinter中 ,提供了许多可在GUI应用程序中使用的小部件。 其中一些主要小部件将在下面讨论:

1) Button

1)按钮

To add a button in the GUI application, we are using this widget in the program.

要在GUI应用程序中添加按钮,我们在程序中使用此小部件。

Syntax:

句法:

button_name=tkinter,Button(
parents_window_name,
text='text_writing',
width='width_of_text',
command='function to call')

2) Radio Button

2)单选按钮

The widgets used to offer multiple options to the user. To add radio button we can simply use RadioButton class.

这些小部件用于向用户提供多个选项。 要添加单选按钮,我们可以简单地使用RadioButton类。

rad1=Radiobutton(
Parents_window_name,
text='text_writing',
value=numerical_value).pack()

We have to give a different value for every radio button, otherwise, they won't work.

我们必须为每个单选按钮指定不同的值,否则它们将无法工作。

3) Listbox

3)列表框

This widget offers a list of options to the user to select any options.

该小部件向用户提供选项列表,以选择任何选项。

Syntax:

句法:

List=Listbox(Parents_window_name)
List.insert(1, text)
List.insert(2, nexttext)

4) Entry

4)进入

This widget provide user to enter multiple line text input.

该小部件提供用户输入多行文本输入的功能。

Syntax:

句法:

label1=Label(Parents_window_name, text='Name').grid(row=0)
label2=Label(Parents_window_name, text='password').grid(row=1)
entry_in_label1 = Entry(Parents_window_name)
entry_in_label2 = Entry(Parents_window_name)
entry_in_label1.grid(row=0, column=1)
entry_in_label2.grid(row=1, column=1)

The implementation of these methods is in the below program.

这些程序的实现在下面的程序中。

Program:

程序:

import tkinter
from tkinter import *
Includehelp.title('Includehelp')
rad1=Radiobutton(Includehelp, text='Content', value=1).pack()
rad2=Radiobutton(Includehelp, text='Software', value=2).pack()
lebel=tkinter.Button(Includehelp,text='Welcome to Includehelp', width=100).pack()
Includehelp.mainloop()

tkinter module example 1

翻译自: https://www.includehelp.com/python/gui-programming-in-python-using-tkinter-module.aspx



推荐阅读
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 解决PHP与MySQL连接时出现500错误的方法
    本文详细探讨了当使用PHP连接MySQL数据库时遇到500内部服务器错误的多种解决方案,提供了详尽的操作步骤和专业建议。无论是初学者还是有经验的开发者,都能从中受益。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 根据最新发布的《互联网人才趋势报告》,尽管大量IT从业者已转向Python开发,但随着人工智能和大数据领域的迅猛发展,仍存在巨大的人才缺口。本文将详细介绍如何使用Python编写一个简单的爬虫程序,并提供完整的代码示例。 ... [详细]
  • 资源推荐 | TensorFlow官方中文教程助力英语非母语者学习
    来源:机器之心。本文详细介绍了TensorFlow官方提供的中文版教程和指南,帮助开发者更好地理解和应用这一强大的开源机器学习平台。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • python的交互模式怎么输出名文汉字[python常见问题]
    在命令行模式下敲命令python,就看到类似如下的一堆文本输出,然后就进入到Python交互模式,它的提示符是>>>,此时我们可以使用print() ... [详细]
  • 本文详细介绍了如何使用PHP检测AJAX请求,通过分析预定义服务器变量来判断请求是否来自XMLHttpRequest。此方法简单实用,适用于各种Web开发场景。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
author-avatar
houxue
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有