使用tkinter模塊在Python中進行GUI編程

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541835.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541835.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541835.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

php輕博客社區視頻教程,輕博客主題 - SEO極致優化的ZBLOG輕博客主題

zblog自適應輕博客主題,簡潔、輕巧、極致優化~QQ群:457320274 (問題反饋以及其他鏈接交換等) 交流社區:https://www.bxiu.net/ (有問題可以求助交流)更新記錄:2021.02.22 v2.8 更新內容:1、新增分類自定義標題&#xf…

Composer學習之————Ubuntu14.04下安裝Composer

下載Composer: curl -sS https://getcomposer.org/installer | php 安裝Composer: /usr/bin/php composer.phar --version 設置全局命令: sudo mv composer.phar /usr/local/bin/composer 查看是否安裝與設置成功: composer -vers…

u盤啟動iso 開源_啟動和維護開源項目

u盤啟動iso 開源Lets talk about how to start an open-source project? The process can be classified as in three phases, 讓我們談談如何啟動一個開源項目? 該過程可以分為三個階段, Individual senses the need of the project: This is the pha…

java如何解決高并發癥,JAVA線上故障緊急處理詳細過程!

鏈接:https://fredal.xin/java-error-check?hmsrtoutiao.io&utm_mediumtoutiao.io&utm_sourcetoutiao.io線上故障主要會包括 CPU、磁盤、內存以及網絡問題,而大多數故障可能會包含不止一個層面的問題,所以進行排查時候盡量四個方面依…

程序員如何談加薪?

如果你對現在公司很滿意,只是覺得薪資太低,那么可以先和你的主管聊聊。 首先,講一講自己最近在工作上的成長,看主管是否認同; 然后,從能力提升角度,向主管要一個更大的發展空間和更大的業務挑戰…

php有多少魔術方法,PHP常用的幾個魔術方法

常用的魔術方法有:__Tostring () __Call() __autoLoad() __ clone() __GET() __SET() __isset() __unset()1.__Tostring()用于定義輸出對象引用時調用常用于打印一些對象的信息必須有返回值eg:有一個persion類Persion per new persion()Echo per; //直接…

python常用語法和示例_使用Python中的示例進行輸入和輸出操作

python常用語法和示例A Program needs to interact with the user to accomplish the desired task; this is done using Input-Output facility. Input means the data entered by the user of the program. In python, we have input() and raw_input ( ) function available…

關于node.js和npm 和nvm_byKL

關于node.js和npm 和nvm Node 是一個服務器端 JavaScript 解釋器,Node 本身運行 V8 JavaScript。V8 JavaScript 引擎是 Google 用于其 Chrome 瀏覽器的底層 JavaScript 引擎。 NPM是隨同NodeJS一起安裝的包管理工具,能解決NodeJS代碼部署上的很多問題&am…

php 查看擴展 代碼,[擴展推薦] 使用 PHP Insights 在終端查看 PHP 項目代碼質量

PHP Insights 是一個由 Nuno Maduro 發布的、可在控制臺進行 PHP 即時質量檢查的拓展包。在項目的 readme 文件中,可以發現 PHP Insights 的主要功能包含:代碼質量 與 代碼風格 分析一個針對于代碼 結構 和 復雜度 的漂亮的預覽界面在 Laravel、Symfon…

航空機票預訂c#代碼_航空公司座位預訂問題的C ++程序

航空機票預訂c#代碼Problem statement: Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numbering as follows: 問題陳述:編寫一個程序來分配飛機上的乘客座位。 假設小型飛機的座位編號如下: 1 A B C…

linux命令之which

which這個命令可以說并不常用,它的作用是查看可執行文件的位置,并返回第一個搜索結果。可執行文件也就是指的某個系統命令,但是這個命令的位置必須是在PATH路徑里存在的。截圖中 ,pwd的位置在/bin/pwd,當然,這個路徑是…

線性代數向量乘法_向量的標量乘法| 使用Python的線性代數

線性代數向量乘法Prerequisite: Linear Algebra | Defining a Vector 先決條件: 線性代數| 定義向量 Linear algebra is the branch of mathematics concerning linear equations by using vector spaces and through matrices. In other words, a vector is a mat…

sonar掃描普通JAVA執行,SonarQube掃描源代碼的方法

SonarQube掃描源代碼的方法雷建鋒一、分析源代碼綜述一旦成功安裝了SonarQube平臺,您就可以開始安裝一個分析器并開始創建項目了。在第一次分析時,該平臺會自動創建一個項目。如果您需要在第一個分析之前在項目上設置一些配置,那么您可以選擇…

html的學習思維導圖

轉載于:https://www.cnblogs.com/lingdublog/p/6438088.html

php語言冒泡法,PHP實現冒泡排序算法的案例

PHP實現冒泡排序算法的案例發布時間:2020-10-23 17:39:38來源:億速云閱讀:84作者:小新這篇文章主要介紹PHP實現冒泡排序算法的案例,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定…

線性代數分塊矩陣求逆矩陣_單位矩陣屬性(AI = A)| 使用Python的線性代數

線性代數分塊矩陣求逆矩陣Prerequisites: 先決條件: Defining Matrix 定義矩陣 Identity matrix 身份矩陣 numpy.matmul( ) matrix multiplication numpy.matmul()矩陣乘法 In linear algebra, the identity matrix, of size n is the n n square matrix with one…

MySQL5.7.17的簡單配置文件

#編譯安裝mysql5.7.17 [rootweb_1 data]# cat ../my.cnf [client]port3307socket/data/3307/mysql.sock[mysqld]user mysqlbasedir /usr/local/mysqldatadir /data/3307/dataport3307server-id 1socket/data/3307/mysql.sockcharacter-set-server utf8log-error /data/33…

cubic-bezier_帶CSS中的示例的cube-bezier()函數

cubic-bezierIntroduction: 介紹: How many times have we come across the word function? Well, it would not be wrong to say a lot. The fact that functions are used in web development while developing a website or web page is very important. There…

php時間調用最簡單的,PHP調用時間通過引用不可避免?

給定以下接口:interface ISoapInterface {public static function registerSoapTypes( &$wsdl );public static function registerSoapOperations( &$server );}以及以下代碼:$soapProvider array( "FilePool", "UserList" );foreach( $soapProvi…

上手Caffe(一)

author:oneBite 本文記錄編譯使用caffe for windows 使用環境 VS2013 ultimate,win7 sp1,caffe-windows源碼(從github上下載caffe的windows分支,下載解壓之后,不要改變原有的目錄結構,因為solution rebuild時會使用文件的相對路徑…