使用 simpledialog.askstring() 时出现错误“‘str’对象不可调用”

问题描述 投票:0回答:1

我的代码:


class Punkty_Edycja_Frame(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.grid(row = 1, column = 0, sticky='nesw')
        self.rowconfigure((0, 1, 2), weight=1, uniform='a')
        self.columnconfigure((0, 1), weight=1, uniform='a')
        
        self.punkty_listbox = tk.Listbox()
        self.punkty = {}
        
        #zapisanie punktu
        ttk.Button(self, text = "Zapisz punkt", command = self.zapisz_punkt
                   ).grid(row = 0, column = 0, sticky='nesw')
        
    def pobierz_zmienne_q(self):
        return (0, 0, 0, 0, 0, 0)

    def zapisz_punkt(self):
        nazwa = sd.askstring('T', "Wpisz nazwę punktu:")
        if nazwa != '' and nazwa != None:
            self.punkty_listbox.insert(tk.END, nazwa)
            self.punkty[nazwa] = self.pobierz_zmienne_q()
        print(self.punkty)

我收到“TypeError: 'str' object is not callable”,特别是在 zapisz_punkt() 方法的“nazwa = sd.askstring('T', "Wpisz nazwę punktu:")”行上。我不知道为什么。

变量“nazwa”应该从弹出窗口中给我一个字符串。我确保我正确导入了 simpledialog 。其他模块(例如消息框和文件对话框)可以工作(在其他按钮上)。即使我在 init 方法中调用它,我也会收到此错误。我不知道我在哪里调用这个字符串。

这是错误的完整堆栈跟踪:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\me\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1967, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "c:\users\me\desktop\aplikacja gui\pusty.py", line 230, in zapisz_punkt
    nazwa = sd.askstring('T', "Wpisz nazwę punktu:")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Programs\Python\Python312\Lib\tkinter\simpledialog.py", line 411, in askstring
    d = _QueryString(title, prompt, **kw)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Programs\Python\Python312\Lib\tkinter\simpledialog.py", line 388, in __init__
    _QueryDialog.__init__(self, *args, **kw)
  File "C:\Users\me\AppData\Local\Programs\Python\Python312\Lib\tkinter\simpledialog.py", line 283, in __init__
    Dialog.__init__(self, parent, title)
  File "C:\Users\me\AppData\Local\Programs\Python\Python312\Lib\tkinter\simpledialog.py", line 109, in __init__
    Toplevel.__init__(self, master)
  File "C:\Users\me\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2700, in __init__
    self.title(root.title())
               ^^^^^^^^^^^^
TypeError: 'str' object is not callable
python class oop tkinter simpledialog
1个回答
0
投票

取下末端的支架。应该是

self.punkty[nazwa] = self.pobierz_zmienne_q

© www.soinside.com 2019 - 2024. All rights reserved.