【问题标题】:Manipulation of a button property by its own command callback function [duplicate]通过自己的命令回调函数操作按钮属性[重复]
【发布时间】:2020-12-19 13:48:22
【问题描述】:

我在 python 中实现了以下代码来操作按下按钮的自己的属性(此处为 button.text)。 执行代码时出现以下错误:“AttributeError: 'Gui' object has no attribute 'button'”。对于我的示例来说重要的是,该按钮是类的一部分,并在其 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()

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

【问题讨论】:

    标签: python python-3.x button tkinter callback


    【解决方案1】:

    删除此行中的()

    command=self.connect_disconnct(),
    

    你想给按钮一个函数来调用。 () 调用该函数,在设置属性self.button 之前执行它。即使在方法中没有对self.button 的引用,您也会将方法的返回值交给Button,在本例中为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()
    

    【讨论】:

      【解决方案2】:

      除了@Axe319 的回答还有几点:

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

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

      至于从按钮获取文本:

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

      最后出现拼写错误:

      self.button.test == connect:
      #             x
      

      【讨论】:

        猜你喜欢
        • 2012-05-13
        • 2015-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-28
        • 2018-06-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多