一个方法中的多个按钮Python 3

2024-09-27 00:14:53 发布

您现在位置:Python中文网/ 问答频道 /正文

这些是我的按钮,我想在鼠标进入某个按钮时更改我的状态栏文本。我只想用鼠标输入按钮的一种方法。怎么做?在

self.connectBtn = tk.Button(self.master, text="CONNECT", width=8)
self.connectBtn.place(x=10, y=100)

self.backupBtn = tk.Button(self.master, text="BACKUP", width=8)
self.backupBtn.place(x=80, y=100)

self.copyBtn = tk.Button(self.master, text="COPY", width=8)
self.copyBtn.place(x=10, y=130)

self.moveBtn = tk.Button(self.master, text="MOVE", width=8)
self.moveBtn.place(x=80, y=130)

for self.button in [self.connectBtn, self.backupBtn, self.copyBtn, self.moveBtn]:
    self.button.bind("<Enter>", self.mouseOver)
    self.button.bind("<Leave>", self.mouseLeave)

我的鼠标搜索方法

^{pr2}$

如果鼠标输入了备份按钮,则状态栏中的文本应为“备份所选数据库”。然后在其他按钮上。我不知道用什么来做那些按钮。谢谢!在


Tags: textselfmasterplacebutton鼠标width按钮
1条回答
网友
1楼 · 发布于 2024-09-27 00:14:53

这个SO help page建议发布一个最小的、完整的、可验证的示例(很少有人会这样做;-)。我建议做同样的实验。当从空闲或控制台运行时(为了有一个可以打印的地方),下面将打印“Success!”每次鼠标进入按钮。在

import tkinter as tk
root = tk.Tk()
but = tk.Button(root, text = 'Hi')
but.pack()
print(but)
def cb(event):
    if event.widget == but:
        print('Success!')
but.bind('<Enter>', cb)
root.mainloop()

关键点是:回调得到一个参数,一个事件对象;事件有大约20个属性,其中一个是小部件;可以比较小部件是否相等。应用于您的代码,下面的代码将(应该)起作用。在

^{pr2}$

不过,我个人会进一步考虑代码。在

action = {self.backupBtn: 'Backs up', self.connectBtn: 'Connects to',
    self.moveBtn: 'Copies', self.moveBtn: 'Moves'}
def mouseOver(self, event):
    self.status['text'] = "%s the selected database." % action[event.widget]

相关问题 更多 >

    热门问题