Python:“NoneType”对象没有属性“get”
我正在使用tkinter在python中创建GUI,运行时遇到了问题。我有一个输入框小部件、一个radiobutton小部件和一个button小部件。当我按下按钮时,我希望用户在输入框中键入一个数字,然后从单选按钮列表中选择一个选项。当用户按下按钮时,我希望检索值并显示在另一个框架中进行测试。取而代之的是,当按下按钮时,出现错误“NoneType”对象没有属性“get”。错误是指输入框内的值:self.tune\u entry 我的守则如下: 萨乌梅因酒店Python:“NoneType”对象没有属性“get”,python,python-3.x,tkinter,nonetype,tkinter-entry,Python,Python 3.x,Tkinter,Nonetype,Tkinter Entry,我正在使用tkinter在python中创建GUI,运行时遇到了问题。我有一个输入框小部件、一个radiobutton小部件和一个button小部件。当我按下按钮时,我希望用户在输入框中键入一个数字,然后从单选按钮列表中选择一个选项。当用户按下按钮时,我希望检索值并显示在另一个框架中进行测试。取而代之的是,当按下按钮时,出现错误“NoneType”对象没有属性“get”。错误是指输入框内的值:self.tune\u entry 我的守则如下: 萨乌梅因酒店 import os import tk
import os
import tkinter as tk
from tkinter import ttk
from tkinter import font
import SA_gui
def main():
x_vals = [0,1,2,3,4]
y_vals = [0,1,2,3,4]
root = SA_gui.tk.Tk()
UI = SA_gui.Window(root, x_vals, y_vals)
root.mainloop()
if __name__ == "__main__":
main()
萨乌圭
import os
import tkinter as tk
from tkinter import ttk
from tkinter import font
# Class to define, setup, and build the GUI
class Window:
# Dimensions of the GUI
HEIGHT = 600
WIDTH = 1200
# Colors for main layout
bg_color = "#B0E0E6"
frame_color1 = "#73B1B7"
white_color = "#FFFFFF"
def __init__(self, master, x_vals, y_vals):
# Take in the lists of files for later use
self.x_vals = x_vals
self.y_vals = y_vals
#--------------------------------------------------------------
# Define and create the window
self.master = master
master.title("Signal Analysis")
master.geometry("{}x{}".format(Window.WIDTH, Window.HEIGHT))
# Create and place the background frame
self.bg_frame = tk.Frame(self.master, bg=Window.bg_color, bd=5)
self.bg_frame.place(relwidth=1, relheight=1)
# Create the main title
self.main_title = tk.Label(self.bg_frame, text="Software Defined Radio Signal Analysis",
bg=Window.bg_color, font=("Courier", 14))
self.main_title.pack(side="top")
#--------------------------------------------------------------
# Create and place the frame for tuning
self.tune_frame = tk.Frame(self.bg_frame, bg=Window.frame_color1, bd=4)
self.tune_frame.place(relx=0.05, rely=0.1, relwidth=0.2428, relheight=0.8)
# Create and place the title for the tuning frame
self.tune_title = tk.Label(self.tune_frame, text="Tune", bg=Window.frame_color1, font=
("Courier", 11))
self.tune_title.place(relwidth=1, anchor="nw")
# Create and place the contents of the tuning frame
self.tune_cont = tk.Frame(self.tune_frame, bg=Window.white_color, bd=4)
self.tune_cont.place(relx=0.05, rely=0.05, relwidth=0.9, relheight=0.925)
#Label for frequency entry
self.tune_label = tk.Label(self.tune_cont, text='Enter carrier frequency: (kHz)',
bg=Window.white_color)
self.tune_label.place(relx=0.025, rely=0)
#Entry Box for frequency entry
self.tune_entry = tk.Entry(self.tune_cont)
self.tune_entry.place(relx=0.025, rely=0.075, relwidth=0.95, relheight=0.05)
#Label for divider
self.tune_div = ttk.Separator(self.tune_cont, orient="horizontal")
self.tune_div.place(rely=0.175, relwidth=1)
#Label for display mode
self.disp_label = tk.Label(self.tune_cont, text='Select Display:', bg=Window.white_color)
self.disp_label.place(relx=0.025, rely=0.2)
#Variable for radiobuttons
self.var = tk.IntVar(self.tune_cont).set("1")
#Radio Button for Spectral Analysis
self.SA_select = tk.Radiobutton(self.tune_cont, text="Spectral
Analysis",bg=Window.white_color, padx=20, variable=self.var, value=1)
self.SA_select.place(relx=0.025, rely=0.275)
#Radio Button for Option 2
self.opt2_select = tk.Radiobutton(self.tune_cont, text="Option 2",bg=Window.white_color,
padx=20, variable=self.var, value=2)
self.opt2_select.place(relx=0.025, rely=0.35)
#Radio Button for Option 3
self.opt3_select = tk.Radiobutton(self.tune_cont, text="Option 3",bg=Window.white_color,
padx=20, variable=self.var, value=3)
self.opt3_select.place(relx=0.025, rely=0.425)
#Button for selection
self.tune_button = ttk.Button(self.tune_cont, text="Enter", command=lambda:
self.print_selected(self.var.get(), self.tune_entry.get()))
self.tune_button.place(relx= 0.775, rely=0.9, relwidth=0.2, relheight=0.075)
#-----------------------------------------------------------------
# Create and place the frame for the plot
self.plot_frame = tk.Frame(self.bg_frame, bg=Window.frame_color1, bd=4)
self.plot_frame.place(relx=0.3428, rely=0.1, relwidth=0.6071, relheight=0.8)
# Create and place the title for the plot frame
self.plot_title = tk.Label(self.plot_frame, text="Plot", bg=Window.frame_color1, font=
("Courier", 11))
self.plot_title.place(relwidth=1, anchor="nw")
# Create and place the contents of the plot frame
self.plot_cont = tk.Frame(self.plot_frame, bg=Window.white_color, bd=4)
self.plot_cont.place(relx=0.025, rely=0.05, relwidth=0.95, relheight=0.925)
def print_selected(self, disp, freq):
if disp == 1:
disp_mode = "Spectral Analysis"
elif disp == 2:
disp_mode = "Option 2"
else:
disp_mode = "Option 3"
#Label for this test
self.prnt_label = tk.Label(self.plot_cont, text="Display: " + disp_mode + ", Center Freq: " +
freq, bg=Window.white_color)
self.prnt_label.place(relx=0.025, rely=0.2)
非常感谢为解决此问题提供的任何帮助 考虑以下代码:
self.var = tk.IntVar(self.tune_cont).set("1")
无论何时执行x=y.z,python都会将z的返回值指定给x。因此,在代码中,您将.set1的结果分配给self.var。set方法返回None,因此self.var为None。因此,当您稍后尝试调用self.var.get时,它与执行None.get相同
如果要在创建变量时初始化变量,则无需调用set。此外,虽然它可以传递字符串,但如果要设置IntVar,实际上应该将其设置为整数
self.var = tk.IntVar(value=1)
请发布并将回溯的全文添加为格式化文本。您确定问题出在self.tune\u条目上吗?这似乎极不可能。相反,它应该发生在self.var上@BryanOakley我不确定它的self.tune\u条目可能是self.var。我也会调查的谢谢@MattDMo只说“非类型”对象在带有按钮的行上没有属性“get”command@jstraugh是的,我不想通过阅读将近150行代码来找到那一行。下次,请将回溯的全文作为格式化文本包含在问题本身中。另外,请阅读关于s的文章,在你的问题中发表一篇文章,而不是贴一堵无关代码的墙。有一半的时间,制作MRE的过程会引导你找到答案,所以这是一件非常值得做的事情。