如何解决Python:“ NoneType”对象没有属性“ get”
我正在使用tkinter在python中创建GUI,并且在运行它时遇到了麻烦。我有一个输入框小部件,一个单选按钮小部件和一个按钮小部件。当我按下按钮时,我想要的是用户在输入框中键入数字,然后从单选按钮列表中选择一个选项。当用户按下按钮时,我希望检索这些值并将其显示在另一个框架中进行测试。我得到的是,当按下按钮时,出现错误'NoneType' object has no attribute 'get'
。错误是指输入框内的值:self.tune_entry
我的代码如下:
SA_main.py
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,4]
root = SA_gui.tk.Tk()
UI = SA_gui.Window(root,x_vals,y_vals)
root.mainloop()
if __name__ == "__main__":
main()
SA_gui.py
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,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",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",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",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",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",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,bd=4)
self.plot_frame.place(relx=0.3428,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",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,bd=4)
self.plot_cont.place(relx=0.025,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
。因此,在您的代码中,您将.set("1")
的结果辅助为self.var
。 set
方法将返回None
,因此self.var
为None
。因此,当您以后尝试调用self.var.get()
时,就和None.get()
一样。
如果要在创建时初始化变量,则无需调用set
。另外,虽然它可以传递字符串,但是如果您要设置IntVar
,则应该将其设置为整数。
self.var = tk.IntVar(value=1)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。