Python:“ NoneType”对象没有属性“ get”

如何解决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.varset方法将返回None,因此self.varNone。因此,当您以后尝试调用self.var.get()时,就和None.get()一样。

如果要在创建时初始化变量,则无需调用set。另外,虽然它可以传递字符串,但是如果您要设置IntVar,则应该将其设置为整数。

self.var = tk.IntVar(value=1)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-