Introduction to Programming Using Python——第9章 笔记

这篇博客介绍了Python的GUI编程库Tkinter,包括其基本概念、如何开始使用Tkinter、事件处理以及主要的Widget类。通过示例代码LISTING 9.1 SimpleGUI.py和9.3 ProcessButtonEventAlternativeCode.py,展示了如何创建和处理GUI事件。此外,还提到了Tkinter提供的各种组件,如Button, Checkbutton, Radiobutton, Label, Entry和Text等。" 116133911,493900,SpringBoot:使用RestController实现响应数据统一,"['Java', 'SpringBoot', 'Web开发', 'REST API', 'AOP']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CHAPTER 9 

GUI PROGRAMMING USING TKINTER

9.1 Introduction

Tkinter (读作 T-K-Inter 是Tk interface的缩略),是Python自带的GUI库。

-----------------------------------------------------------------------------

9.2 Getting Started with Tkinter

LISTING 9.1 SimpleGUI.py

from tkinter import * # Import all definitions from tkinter

window = Tk() # Create a window
label = Label(window, text = "Welcome to Python") # Create a label
button = Button(window, text = "Click Me") # Create a button
label.pack() # Place the label in the window
button.pack() # Place the button in the window

window.mainloop() # Create an event loop

------------------------------------------------------------------------------

9.3 Processing Events

LISTING 9.3 ProcessButtonEventAlternativeCode.py

from tkinter import *

class ProcessButtonEvent:
    def __init__(self):
        window = Tk() # 创建tk类
        btnOK = Button(window, text = "OK",fg = "red", command = self.processOK)
        btnCancel = Button(window, text = "Cancel", bg = "yellow", command = self.processCancel)
        
        btnOK.pack() # Place the OK button in the window
        btnCancel.pack() # Place the Cancel button in the window
        
        window.mainloop() # Create an event loop
    
    def processOK(self):
        print("OK button is clicked")

    def processCancel(self):
        print("Cancel button is clicked")
        
ProcessButtonEvent() # Create an object to invoke __init__method

这段代码,定义了一个class,然后在class的__init__方法里面创建了GUI。函数processOK和函数processCancel都是class中的instance methods。所以class中调用的时候要使用self.processOK和self.processCancel。

使用定义一个class来创建GUI处理事件有两个好处:

  1. class可以将来反复使用。
  2. 所有函数都作为methods的时候,调用和访问函数的时候,作用域都在class内部。

--------------------------------------------

9.4 The Widget Classes


Tkinter提供的Widget Classes

要放置widgets 需要有parent container,所有widgets都有属性可以设置,设置语法如下:

widgetName["propertyName"] = newPropertyValue

btShowOrHide = Button(window, text = "Show", bg = "white") # 创建一个Button widget放置在window中,text属性为Show,bg属性为white
btShowOrHide["text"] = "Hide" # 将text属性改为Hide
btShowOrHide["bg"] = "red" # 将bg属性改为red
btShowOrHide["fg"] = "#AB84F9" # Change fg color to #AB84F9
btShowOrHide["cursor"] = "plus" # Change mouse cursor to plus
btShowOrHide["justify"] = LEFT # Set justify to LEFT 设置对齐方式为左对齐

关于更多属性方法的使用,此书不做介绍,大家可以去查找tkinter参考文档

listing 9.4将介绍 Frame, Button,Checkbutton, Radiobutton, Label, Entry(text field),Message和Text(text area)组件

LISTING 9.4 WidgetsDemo.py


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值