tkinter教程

介绍 (Introduction)

In the first part of our Tkinter tutorial, we created a simple graphical interface having a window and a label. The article explained how to create it and customize it. In the second part of the Tkinter tutorial, where we’ll add different widgets to our window. We will also learn to connect the GUI with the code by getting input from the user.

在Tkinter教程的第一部分中,我们创建了一个具有窗口和标签的简单图形界面。 本文介绍了如何创建和自定义它。 在Tkinter教程的第二部分,我们将在窗口中添加不同的小部件。 我们还将通过获取用户的输入来学习如何将GUI与代码连接起来。

添加功能按钮 (Adding a function button)

Now, let’s add some widgets which enable the user to interact with. Let’s try adding a simple functional button and handle a simple button click event. Exciting, isn’t it? Let’s do a hands-on.

现在,让我们添加一些使用户能够进行交互的小部件。 让我们尝试添加一个简单的功能按钮并处理一个简单的按钮单击事件。 令人兴奋,不是吗? 让我们动手做吧。

Adding a functional button to the window is similar to the process which we did for adding a label widget. We can customize the appearance of the button by changing foreground for a button using `fg` property. Similarly, we can change the background color for the button by the `bg` property. This property can be used to customize any widget. Let’s now add a functional button to the same window which we created and added a label widget:

向窗口添加功能按钮类似于我们添加标签小部件的过程。 我们可以通过使用`fg`属性改变按钮的前景来定制按钮的外观。 同样,我们可以通过`bg`属性来改变按钮的背景色。 此属性可用于自定义任何窗口小部件。 现在,将功能按钮添加到创建和添加标签小部件的同一窗口中:

import tkinter
root_window = tkinter.Tk()
root_window.geometry('950x200')
root_window.title("Tkinter tutorial")
label_widget = tkinter.Label(root_window, text="Build your own GUI with Tkinter", font=("Arial Italic", 40))
label_widget.grid(column=1, row=1)
fn_btn = tkinter.Button(root_window, text="Close", bg="grey", fg="black", font=("Arial Bold", 20))
fn_btn.grid(column=1, row=2)
root_window.mainloop()

The above code creates a functional button widget, based on our specifications. The output looks like:

上面的代码根据我们的规范创建了一个功能性按钮小部件。 输出如下:

Output:

输出:

调整小部件的方向 (Orienting the widgets)

If you look at the above code, we have used the grid() function to specify the position of the widget. In case, this is not used, the widget will not be displayed. In the above example, we have placed the button on the third row of the window, which is 2. If you place the function button on the same row and column as we specified for the label widget, the button widget will be placed on top of the label widget. So, to handle such situations, `tkinter` module offers a method named ‘pack()’. This can be used instead of the grid() method. If your window has more than 2 widgets, we can use the pack() method instead of specifying the co-ordinates manually using the grid method. The pack() method throws widgets one on top of the other that is centered horizontally, based on the order we pack our widgets. This method also adjusts the window’s size according to the widget’s size. Let’s try to implement this function for the above-stated example.

如果您看上面的代码,我们已经使用grid()函数指定了小部件的位置。 如果不使用它,则不会显示小部件。 在上面的示例中,我们将按钮放置在窗口的第三行(即2)上。如果将功能按钮放置在与标签小部件指定的相同的行和列中,则按钮小部件将放置在顶部标签小部件的。 因此,为了处理这种情况,`tkinter`模块提供了一个名为'pack()'的方法。 可以使用它代替grid()方法。 如果您的窗口有两个以上的小部件,我们可以使用pack()方法,而不是使用grid方法手动指定坐标。 根据我们打包小部件的顺序,pack()方法将小部件一个放在水平居中的另一个顶部。 此方法还根据窗口小部件的大小调整窗口的大小。 让我们尝试为上述示例实现此功能。

import tkinter
root_window = tkinter.Tk()
root_window.geometry('950x200')
root_window.title("Tkinter tutorial")
label_widget = tkinter.Label(root_window, text="Build your own GUI with Tkinter", font=("Arial Italic", 40))
fn_btn = tkinter.Button(root_window, text="Close", bg="grey", fg="black", font=("Arial Bold", 20))
label_widget.pack()
fn_btn.pack()
root_window.mainloop()

The output looks like:

输出如下:

Output:

输出:

So, we have discussed how to orient the widgets in our window using the grid() method and pack() method. But, we need to keep in mind not to mix the grid() and pack() methods, both in the same container.

因此,我们讨论了如何使用grid()方法和pack()方法在窗口中定位小部件。 但是,我们需要记住不要在同一容器中混合使用grid()和pack()方法。

Now, we have just created a function button. If we click on it, nothing happens. Let’s now write a code to handle the button click event.

现在,我们刚刚创建了一个功能按钮。 如果单击它,则什么也不会发生。 现在让我们编写一个代码来处理按钮单击事件。

处理功能按钮单击事件 (Handling functional button click event)

In the previous example, we created a Button class instance that is pretty similar to the way in which we created the label widget. Now, to handle the functional button click event, we can add a command argument while creating the button widget. By adding this `command` argument, we can specify the program on how to react after the functional button click occurs. We can do this by adding the below chunk of code:

在前面的示例中,我们创建了一个Button类实例,该实例与创建标签小部件的方式非常相似。 现在,要处理功能性按钮单击事件,我们可以在创建按钮小部件时添加命令参数。 通过添加这个“ command”参数,我们可以指定程序,在发生功能按钮单击后如何做出React。 我们可以通过添加以下代码块来做到这一点:

fn_btn = tkinter.Button(root_window, text="Close", bg="grey", fg="black", font=("Arial Bold", 20), command=root_window.destroy)

In this example, we have handled our button click event by configuring the command argument. We have called the root window’s destroy() method. This makes our window widget to close when the close button is clicked.

在此示例中,我们通过配置命令参数来处理按钮单击事件。 我们已经调用了根窗口的destroy()方法。 这使我们的窗口小部件在单击关闭按钮时关闭。

We have seen samples that barely shows the package’s basic capabilities. Let us dive in deeper to explore the complex and more intuitive Graphical User Interface capabilities of the Tkinter package.

我们看到的样本几乎没有显示该软件包的基本功能。 让我们更深入地研究Tkinter软件包的复杂和更直观的图形用户界面功能。

配置小部件 (Configuring the Widgets)

We can configure the appearance of the widgets during the run-time of our program. This can be done by using the configuring element of the widget. For example, let’s create a window, a label, a button named “Click me”. We can configure the functional button during the run-time of the program by changing the button’s color.

我们可以在程序运行时配置小部件的外观。 这可以通过使用小部件的configure元素来完成。 例如,让我们创建一个窗口,一个标签,一个名为“ Click me”的按钮。 我们可以在程序运行时通过更改按钮的颜色来配置功能按钮。

import tkinter
root_window = tkinter.Tk()
root_window.geometry('950x200')
root_window.title("Tkinter tutorial")
def color_fn_btn():fn_btn.configure(text="Button color changed", bg="blue", fg="white")
label_widget = tkinter.Label(root_window, text="Build your own GUI with Tkinter", font=("Arial Italic", 40))
fn_btn = tkinter.Button(root_window, text="Click here", bg="grey", fg="black", font=("Arial Bold", 20), command=color_fn_btn)
label_widget.pack()
fn_btn.pack()
root_window.mainloop()

The sample output of the above code is:

上面的代码的示例输出是:

Output:

输出:

If you click the “Click me” button, the color of the functional button along with its text changes as follow:

如果单击“单击我”按钮,则功能按钮的颜色及其文本将更改为:

Output:

输出:

The color_fn_btn() function that we added configures the state of the button, once the button click event occurs.

一旦按钮单击事件发生,我们添加的color_fn_btn()函数将配置按钮的状态。

结论 (Conclusion)

I hope, the second part of the Tkinter tutorial made you understand the Tkinter module, better. We see how the Tkinter module has simplified our process in building a Graphical interface for our software. Keep reading the last part of this tutorial to create complex, intuitive GUIs.

希望Tkinter教程的第二部分使您更好地了解Tkinter模块。 我们将看到Tkinter模块如何简化了为软件构建图形界面的过程。 继续阅读本教程的最后一部分,以创建复杂,直观的GUI。

Tkinter tutorial – part 3Tkinter教程–第3部分

翻译自: https://www.journaldev.com/33417/tkinter-tutorial-part-2

tkinter教程

tkinter教程_Tkinter教程–第2部分相关推荐

  1. Python GUI之tkinter窗口视窗教程大集合

    Python GUI之tkinter窗口视窗教程大集合 一.前言 二.Tkinter是什么 三.Tkinter 控件详细介绍 1. Tkinter 模块元素简要说明 2. 常用窗口部件及简要说明: 四 ...

  2. python中tkinter模块窗口操作_Python GUI之tkinter窗口视窗教程大集合(看这篇就够了)...

    本篇博文搬到个人博客:[洪卫の博客](https://sunhwee.com)上面去了,想要获得最佳阅读体验,欢迎前往 [https://sunhwee.com](洪卫の博客), 建议用电脑查看教程文 ...

  3. Tkinter保姆级教程(下)

    继上次肝了几天的Tkinter保姆级教程(上),接下来继续肝教程下,冲冲冲. 目录 Scale控件 Canvas画布控件 Menu菜单控件 Scrollbar滚动条控件 Event事件处理 布局管理器 ...

  4. 在Windows上安装TkInter:完整教程

    在Windows上安装TkInter:完整教程 如果你正在寻找一种可在Windows上使用的GUI库,那么TkInter是一个非常好的选择.但是,如果你手头的资源中没有TkInter,则你需要使用下面 ...

  5. pythontkinter窗口外观样式_Python GUI之tkinter窗口视窗教程大集合(推荐)

    一.前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅. 二.Tkinter是什么 Tkinter 是使用 python 进行窗口视窗设计 ...

  6. Python GUI教程 | Lynda教程 中文字幕

    Python GUI教程 | Lynda教程 中文字幕 Learning Python GUI Programming 课程ID: 604235 时长: 6.1小时 所属类别:Python 了解如何使 ...

  7. ASP.NET Core 异常和错误处理 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 异常和错误处理 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 异常和错误处理 上一章节中,我们学习了 ASP.NET Cor ...

  8. python菜鸟教程字典-python教程菜鸟教程学习路线

    python教程菜鸟教程学习路线,需要学Python 环境搭建.Python 中文编码.Python 基础语法.Python 变量类型.Python 运算符.Python 条件语句.Python 循环 ...

  9. ASP.NET Core macOS 环境配置 - ASP.NET Core 基础教程 - 简单教程,简单编程

    ASP.NET Core macOS 环境配置 - ASP.NET Core 基础教程 - 简单教程,简单编程 原文:ASP.NET Core macOS 环境配置 - ASP.NET Core 基础 ...

  10. ASP.NET Core Razor 标签助手 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Razor 标签助手 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Razor 标签助手 上一章节我们介绍了视图导入,学习了 ...

最新文章

  1. 学习HTML5之塔克大战(详细记录)
  2. ListView属性设置
  3. Android应用开发-广播和服务
  4. JDBC: Java连接MySQL
  5. 基于SVD矩阵分解的用户商品推荐(python实现)
  6. 在html中写三角,css3怎么写三角形?
  7. js中的let和var
  8. NB-IoT成新宠 运营商对物联网充满野心
  9. 如何看到格式化的json文件
  10. python中软件包安装
  11. python测试工程师简历模板_自动化测试工程师简历专业技能怎么写
  12. 华为服务器修改硬盘显示顺序,服务器硬盘顺序
  13. 密钥可以永久激活吗?
  14. 什么是uboot?uboot有什么用?
  15. Python中计算圆的周长,面积
  16. 关于西安电子科技大学821电路的难点辨析(一)---受控源的处理
  17. eclipse下载及安装
  18. ISO8601转换成Date类型
  19. linux网卡驱动离线安装_解决安装Linux之后没有网卡驱动
  20. 盲人张海伦是怎样成为美国防部的程序猿

热门文章

  1. tourex旅游系统 php,TourEx 旅游电商系统B2C_B2B2C v7.0升级V8.0 旅游源码系统无限制版源码工程源码...
  2. TFTP协议下载服务器指定文件夹内的图片
  3. web漏洞扫描器原理_基于指纹识别的漏洞扫描设计
  4. 基于python的漏洞扫描器_基于Python的Web漏洞扫描器
  5. 招聘工作总结(精选多篇)
  6. 在qq2003里实现qq2000的皮肤
  7. Linux系统上QQ闪退的问题
  8. android 性能测试 简介,android性能测试工具traceview简介[转载]
  9. 鸿蒙大陆7.1正式版隐藏英雄,守塔V7.01下载 守塔V7.01正式版 附游戏攻略及隐藏英雄密码 魔兽防守地图 下载-脚本之家...
  10. java.lang.IllegalStateException报警