热门标签 | 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



推荐阅读
  • vue使用
    关键词: ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • C++中的三角函数计算及其应用
    本文介绍了C++中的三角函数的计算方法和应用,包括计算余弦、正弦、正切值以及反三角函数求对应的弧度制角度的示例代码。代码中使用了C++的数学库和命名空间,通过赋值和输出语句实现了三角函数的计算和结果显示。通过学习本文,读者可以了解到C++中三角函数的基本用法和应用场景。 ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • Tkinter Frame容器grid布局并使用Scrollbar滚动原理
    本文介绍了如何使用Tkinter实现Frame容器的grid布局,并通过Scrollbar实现滚动效果。通过将Canvas作为父容器,使用滚动Canvas来滚动Frame,实现了在Frame中添加多个按钮,并通过Scrollbar进行滚动。同时,还介绍了更新Frame大小和绑定滚动按钮的方法,以及配置Scrollbar的相关参数。 ... [详细]
  • c语言\n不换行,c语言printf不换行
    本文目录一览:1、C语言不换行输入2、c语言的 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 如何优化Webpack打包后的代码分割
    本文介绍了如何通过优化Webpack的代码分割来减小打包后的文件大小。主要包括拆分业务逻辑代码和引入第三方包的代码、配置Webpack插件、异步代码的处理、代码分割重命名、配置vendors和cacheGroups等方面的内容。通过合理配置和优化,可以有效减小打包后的文件大小,提高应用的加载速度。 ... [详细]
  • 颜色迁移(reinhard VS welsh)
    不要谈什么天分,运气,你需要的是一个截稿日,以及一个不交稿就能打爆你狗头的人,然后你就会被自己的才华吓到。------ ... [详细]
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社区 版权所有