“ NoneType”对象不可调用

时间:2019-09-18 16:22:46

标签: python tkinter

我编写了以下代码,但由于某些原因,.get()什么也没给我。

我尝试打印fullname.get()的{​​{1}},它给我len并显示以下错误:

0
 <class 'str'>
0

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
TypeError: 'NoneType' object is not callable
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
TypeError: 'NoneType' object is not callable
[Finished in 21.5s]

1 个答案:

答案 0 :(得分:0)

我无法重现您的错误。我怀疑这可能是由import *或您的错误get()方法之一引起的。可能使用get()方法,因为您甚至没有设置条目文本的值,并且可能从中获取Null的值。

请记住,您需要在函数内部而不是在初始化代码的同一位置运行get()方法。

另一个主要问题之一是多次使用Tk()。在构建Tkinter GUI时,您始终只想使用Tk()的一个实例。对于新窗口,请使用Toplevel()

我已清理您的代码以更紧密地遵循PEP8。让我知道是否有帮助。

import tkinter as tk  # use these imports so you do not overwrite other imports.
import tkinter.ttk as ttk  # use these imports so you do not overwrite other imports.
import tkinter.messagebox as messagebox  # avoid import *. Its normally not needed.
from db import DBConnect
from listComp import ListComp


def complaint(master):
        comp = tk.Toplevel(master)
        comp.geometry('1366x768')
        comp.title('Complaint Box')
        comp.configure(background='#2919bf')
        style1 = ttk.Style()
        style1.theme_use('classic')
        tkvar = tk.StringVar(master)
        tkvar.set('')
        labels = ['Full Name:', 'Gender:', 'Choose a Category', 'Comment:']
        choices = {'Student', 'Teacher', 'Management', 'Infrastructure', ''}

        for elem in ['TLabel', 'TButton', 'TRadiobutton']:  # fixed typo
            style1.configure(elem, background='#35a5e1')

        for i in range(4):
            ttk.Label(comp, text=labels[i]).grid(row=i, column=0, padx=200, pady=25)

        popup_menu = tk.OptionMenu(comp, tkvar, *choices)
        popup_menu.grid(row=2, column=1)
        tkvar.trace('w', lambda: change_dropdown(tkvar))  # corrected your trace command.
        fullname = tk.Entry(comp, text='', width=40, font=('Arial', 14))
        fullname.grid(row=0, column=1, columnspan=2)
        span_gender = tk.StringVar(master)
        ttk.Radiobutton(comp, text='Male', value='male', variable=span_gender).grid(row=1, column=1)
        ttk.Radiobutton(comp, text='Female', value='female', variable=span_gender).grid(row=1, column=2)
        comment = tk.Text(comp, width=35, height=5, font=('Arial', 14))
        comment.grid(row=3, column=1, columnspan=2, padx=10, pady=10)
        ttk.Button(comp, text='List Comp.', command=show_list).grid(row=5, column=1)
        ttk.Button(comp, text='Submit Now', command=lambda: save_data(fullname, span_gender,
                                                                      tkvar, comment)).grid(row=5, column=2)


def change_dropdown(tkvar):
    print(tkvar.get())


def save_data(fullname, span_gender, tkvar, comment):
    msg1 = "Failed please fill all information!."
    if len(fullname.get()) == 0 or len(span_gender.get()) == 0:
        messagebox.showinfo(title='Add Info', message=msg1)
    else:
        conn = DBConnect()  # moved conn to the function. No point in opening connection until its needed.
        msg = conn.Add(fullname.get(), span_gender.get(), tkvar.get(), comment.get(1.0, 'end-1c'))
        messagebox.showinfo(title='Add Info', message=msg)
        # you should probably close the connection here.


def show_list():
    # cannot test this function as I do not know what `ListComp()` is doing.
    # That said this function is currently pointless as the local variable
    # will be basically deleted when the function finishes.
    listrequest = ListComp()


root = tk.Tk()
root.geometry('1366x768')
root.title('Complaint Box')
root.configure(background='#2919bf')
style = ttk.Style()
style.theme_use('classic')

for elem in ['TLabel', 'TButton']:
    style.configure(elem, background='#35a5e1')

ttk.Label(root, text="Welcome To Indian Government Complaint Department",
          font="Verdana 24 bold").grid(row=1, column=0, padx=150, pady=10)
ttk.Button(root, text='Register Complaint', command=lambda: complaint(root)).grid(row=5, pady=200, padx=10)
ttk.Button(root, text='View Complaint', command=show_list).grid(row=7, pady=0, padx=10)

root.mainloop()