通过它自己的命令回调函数操作按钮 属性

Manipulation of a button property by its own command callback function

我在 python 中实现了以下代码来操作自己的 属性(这里是 button.text)按下的按钮。 执行代码时出现以下错误:“AttributeError:'Gui' 对象没有属性 'button'”。对于我的示例来说重要的是,该按钮是 class 的一部分并在其 init 中创建。我发现并获得了具有全局定义按钮的其他工作示例 运行。

#!/usr/bin/python3
# -*- coding: iso-8859-1 -*-

import tkinter
from tkinter import *
from tkinter import ttk

class Gui:
    def __init__(self, master):
        self.master = master
        self.frame = Frame(self.master)
        self.frame.pack(fill='both', side='top', expand='True')

        self.button = Button(master=self.frame, 
                                    text='connect', 
                                    height=20, 
                                    width=80, 
                                    padx=1, 
                                    pady=1, 
                                    command=self.connect_disconnct(),                            
                                    compound=LEFT)                                                  
        self.button.grid(row=0, column=0, padx=5, pady=5, sticky='ew')
        mainloop()

    def connect_disconnct(self):
        if  self.button.test == connect:
            print("Button pressed -> connect")
            self.button.text = "disconnect"
        else:
            print("Button pressed -> disconnect")
            self.button.text = "connect"

if __name__ == '__main__':
  form = Tk()                                                                                       
  Gui(form)
  form.mainloop()

如何将自己的按钮元素传递给回调函数,例如调用对象的文本可以在回调函数中更改?

去掉这一行的()

command=self.connect_disconnct(),

你想给按钮一个函数来调用。 () 调用函数,在设置属性 self.button 之前执行它。即使在方法中没有对 self.button 的引用,您也会将 Button 传递给方法的 return 值,在本例中为 None.

此代码还有一些其他问题。要获取按钮文本,您需要这样做 self.button['text'] == 'connect'.

另外,您不能以这种方式设置文本。您需要使用 configure() 方法。

self.button.configure(text="disconnect")

此外,您对 mainloop() 的调用过多。

完整代码:

import tkinter
from tkinter import *
from tkinter import ttk

class Gui:
    def __init__(self, master):
        self.master = master
        self.frame = Frame(self.master)
        self.frame.pack(fill='both', side='top', expand='True')

        self.button = Button(master=self.frame, 
                                    text='connect', 
                                    height=20, 
                                    width=80, 
                                    padx=1, 
                                    pady=1, 
                                    command=self.connect_disconnct,                            
                                    compound=LEFT)                                                  
        self.button.grid(row=0, column=0, padx=5, pady=5, sticky='ew')

    def connect_disconnct(self):
        if self.button['text'] =='connect':
            print("Button pressed -> connect")
            self.button.configure(text="disconnect")
        else:
            print("Button pressed -> disconnect")
            self.button.configure(text="connect")

if __name__ == '__main__':
    form = Tk()                                                                                       
    Gui(form)
    form.mainloop()

除了@Axe319 的回答之外还有一些事情:

您使用错误的语法来更改按钮文本,正确的语法是:

self.button.config(text = "disconnect")

关于从按钮获取文本:

self.button['text'] == 'connect':

终于有个拼写错误:

self.button.test == connect:
#             x