📌  相关文章
📜  Tkinter – 在悬停时更改其属性的按钮

📅  最后修改于: 2022-05-13 01:55:41.936000             🧑  作者: Mango

Tkinter – 在悬停时更改其属性的按钮

Python为开发 GUI(图形用户界面)提供了多种选择。在所有 GUI 方法中, tkinter是最常用的方法。它是Python附带的 Tk GUI 工具包的标准Python接口。 Python和tkinter是创建 GUI 应用程序的最快和最简单的方法。使用tkinter创建 GUI 是一项简单的任务。

在本文中,我们将创建一个在悬停时更改其属性的按钮。

循序渐进的方法:

  • 导入tkinter模块
Python3
# import required module
from tkinter import *


Python3
# function to change properties of button on hover
def changeOnHover(button, colorOnHover, colorOnLeave):
  
    # adjusting backgroung of the widget
    # background on entering widget
    button.bind("", func=lambda e: button.config(
        background=colorOnHover))
  
    # background color on leving widget
    button.bind("", func=lambda e: button.config(
        background=colorOnLeave))


Python3
# Driver Code
root = Tk()
  
# create button
# assign button text along
# with background color
myButton = Button(root,
                  text="On Hover - Background Change",
                  bg="yellow")
myButton.pack()
  
# call function with background
# colors as argument
changeOnHover(myButton, "red", "yellow")
  
root.mainloop()


Python3
# import required module
from tkinter import *
  
  
# function to change properties of button on hover
def changeOnHover(button, colorOnHover, colorOnLeave):
  
    # adjusting backgroung of the widget
    # background on entering widget
    button.bind("", func=lambda e: button.config(
        background=colorOnHover))
  
    # background color on leving widget
    button.bind("", func=lambda e: button.config(
        background=colorOnLeave))
  
  
# Driver Code
root = Tk()
  
# create button
# assign button text along
# with background color
myButton = Button(root,
                  text="On Hover - Background Change",
                  bg="yellow")
myButton.pack()
  
# call function with background
# colors as argument
changeOnHover(myButton, "red", "yellow")
  
root.mainloop()


  • 创建一个通用函数来改变每个按钮的悬停能力。

config()方法用于更改任何小部件的属性。请注意,在config()方法中bgbackground是两个不同的选项, background表示背景颜色。

句法:

widget.config(**options)

下面是实现:

蟒蛇3

# function to change properties of button on hover
def changeOnHover(button, colorOnHover, colorOnLeave):
  
    # adjusting backgroung of the widget
    # background on entering widget
    button.bind("", func=lambda e: button.config(
        background=colorOnHover))
  
    # background color on leving widget
    button.bind("", func=lambda e: button.config(
        background=colorOnLeave))
  • 在驱动程序代码中创建一个按钮并调用显式函数。

蟒蛇3

# Driver Code
root = Tk()
  
# create button
# assign button text along
# with background color
myButton = Button(root,
                  text="On Hover - Background Change",
                  bg="yellow")
myButton.pack()
  
# call function with background
# colors as argument
changeOnHover(myButton, "red", "yellow")
  
root.mainloop()

以下是基于上述方法的完整程序:

蟒蛇3

# import required module
from tkinter import *
  
  
# function to change properties of button on hover
def changeOnHover(button, colorOnHover, colorOnLeave):
  
    # adjusting backgroung of the widget
    # background on entering widget
    button.bind("", func=lambda e: button.config(
        background=colorOnHover))
  
    # background color on leving widget
    button.bind("", func=lambda e: button.config(
        background=colorOnLeave))
  
  
# Driver Code
root = Tk()
  
# create button
# assign button text along
# with background color
myButton = Button(root,
                  text="On Hover - Background Change",
                  bg="yellow")
myButton.pack()
  
# call function with background
# colors as argument
changeOnHover(myButton, "red", "yellow")
  
root.mainloop()

输出: