tkinter类对象未定义

如何解决tkinter类对象未定义

我对Python相当陌生,这是我的第一个使用tkinter的项目。除了一个例外,我的整个项目工作正常。我已经将tkinter代码构建到一个类中,并且可以正常工作,但是我无法弄清楚如何从类外部调用方法。

在main的以下行上创建对象时,出现NameError:未定义名称“ robotGUI”

class botGUI:
    def __init__(self):
        #Init Code

    def updateStatus(self):
        #Code Here

robotGUI = botGUI()

如果我将变量“ robotGUI”初始化为“无”,则代码将运行,但是当我稍后尝试访问其方法之一时,将得到AttributeError:'NoneType'对象没有属性'doSomething'。似乎没有创建robotGUI对象,但我不明白为什么。

我到处搜索,找到了一些接近的答案,但没有什么与这个问题完全相关。我还有很多其他的类在此程序中可以正常工作,因此我确定它与tkinter有关,并且其内部mainloop只是无法指出它。

以下是我大大简化的代码显示的问题:

#!/usr/bin/env python3

#Imports
import socket,select,errno,sys,queue,time,threading,cv2
from tkinter import *
from tkinter import font
from PIL import Image,ImageTk

#GUI
class botGUI:

    def __init__(self):

        #Create the Window Object and Setup the Window
        self.window = Tk()
        self.window.geometry("800x480+0+0")
        self.window.overrideredirect(True)
        self.window.fullScreenState = False

        #Code to Generate Gaphics Here .....        

        #Call Repeating Status Update Script and Start the Main Loop
        self.updateStatus()
        self.window.mainloop()    

    def updateStatus(self):

        #Code to Handle Updating Screen Objects Here ....    
        print("Update Status Running")

        #Set this function to be called again
        self.window.after(1000,lambda: self.updateStatus())
        

    def doSomething(self,myStr):

        #Code to change something on the screen ...
        print(f"Command: {str(myStr)}")

    def doSomethingElse(self,myStr):

        #Code to change something on the screen ...
        print(f"Command: {str(myStr)}")
        


#Main Task - Since tKinter is running in the main loop,all of the main loop code is moved to here
def main_loop():

    global robotGUI
    robotDataReceived = True #This is only for this posting

    #Main Loop
    while True:

        #If Incoming Data from Robot,Get and Process It!
        if robotDataReceived:
            robotCmdHandler()
            
        #Anti Blocking Delay (Much shorter,set higher for this post)
        time.sleep(2)


#Robot Command Handler
def robotCmdHandler():

    global robotGUI

    #Code to get a command string and process it goes here .....
    cmd = "dosomething"  #Temporary for this post

    #Handle command
    if (cmd == "dosomething"):
        print("Processing Command")
        robotGUI.doSomething("Do This")


if __name__ == '__main__':

    global robotGUI
    robotGUI = None

    #Create and Start Threads
    t1 = threading.Thread(target=main_loop,name='t1')
    t1.start()            

    #Create GUI Object
    robotGUI = botGUI()

    #Wait until threads are finished
    t1.join() 

   

                         

解决方法

self.window.mainloop()删除呼叫botGUI.__init__(),然后您可以:

  • 创建botGUI的实例:robotGUI = botGUI()
  • 创建线程并启动它
  • 致电roboGUI.window.mainloop()

下面是修改后的代码:

#!/usr/bin/env python3

#Imports
import socket,select,errno,sys,queue,time,threading,cv2
from tkinter import *
from tkinter import font
from PIL import Image,ImageTk

#GUI
class botGUI:

    def __init__(self):

        #Create the Window Object and Setup the Window
        self.window = Tk()
        self.window.geometry("800x480+0+0")
        self.window.overrideredirect(True)
        self.window.fullScreenState = False

        #Code to Generate Gaphics Here .....        

        #Call Repeating Status Update Script and Start the Main Loop
        self.updateStatus()
        #self.window.mainloop()    

    def updateStatus(self):

        #Code to Handle Updating Screen Objects Here ....    
        print("Update Status Running")

        #Set this function to be called again
        self.window.after(1000,lambda: self.updateStatus())
        

    def doSomething(self,myStr):

        #Code to change something on the screen ...
        print(f"Command: {str(myStr)}")

    def doSomethingElse(self,myStr):

        #Code to change something on the screen ...
        print(f"Command: {str(myStr)}")
        


#Main Task - Since tKinter is running in the main loop,all of the main loop code is moved to here
def main_loop():

    #global robotGUI
    robotDataReceived = True #This is only for this posting

    #Main Loop
    while True:

        #If Incoming Data from Robot,Get and Process It!
        if robotDataReceived:
            robotCmdHandler()
            
        #Anti Blocking Delay (Much shorter,set higher for this post)
        time.sleep(2)


#Robot Command Handler
def robotCmdHandler():

    #global robotGUI

    #Code to get a command string and process it goes here .....
    cmd = "dosomething"  #Temporary for this post

    #Handle command
    if (cmd == "dosomething"):
        print("Processing Command")
        robotGUI.doSomething("Do This")


if __name__ == '__main__':

    #Create GUI Object
    robotGUI = botGUI()

    #Create and Start Threads
    t1 = threading.Thread(target=main_loop,name='t1')
    t1.start()            

    # start the GUI main loop
    robotGUI.window.mainloop()

    #Wait until threads are finished
    t1.join()
,

您必须在所有类似功能之外定义robotGUI:

robotGUI = None
def main_loop():

     global robotGUI
     robotDataReceived = True #This is only for this posting

     #Main Loop
     while True:

         #If Incoming Data from Robot,Get and Process It!
         if robotDataReceived:
             robotCmdHandler()
        
         #Anti Blocking Delay (Much shorter,set higher for this post)
         time.sleep(2)

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?