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



推荐阅读
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社区 版权所有