第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

tkinter 類(lèi)對(duì)象未定義

tkinter 類(lèi)對(duì)象未定義

夢(mèng)里花落0921 2023-10-05 16:45:02
我對(duì) Python 相當(dāng)陌生,這是我使用 tkinter 的第一個(gè)項(xiàng)目。我的整個(gè)項(xiàng)目運(yùn)行良好,只有一個(gè)例外。我已將 tkinter 代碼構(gòu)建到一個(gè)類(lèi)中,一切正常,但我不知道如何從類(lèi)外部調(diào)用這些方法。當(dāng)我在 main 中的以下行創(chuàng)建對(duì)象時(shí),出現(xiàn) NameError: name 'robotGUI' is not Definedclass botGUI:    def __init__(self):        #Init Code    def updateStatus(self):        #Code HererobotGUI = botGUI()如果我將變量“robotGUI”初始化為 None,代碼就會(huì)運(yùn)行,但是當(dāng)我稍后嘗試訪問(wèn)其方法之一時(shí),我會(huì)收到 AttributeError: 'NoneType' object has no attribute 'doSomething'。似乎沒(méi)有創(chuàng)建 robotsGUI 對(duì)象,但我不明白為什么。我到處搜索并找到了一些接近的答案,但沒(méi)有任何與這個(gè)問(wèn)題完全相關(guān)的答案。我有一些其他類(lèi)在這個(gè)程序中運(yùn)行得很好,所以我確信它與 tkinter 和它的內(nèi)部主循環(huán)有關(guān),只是無(wú)法確定它。這是我大大減少和簡(jiǎn)化的代碼,顯示了問(wèn)題:#!/usr/bin/env python3#Importsimport socket, select, errno, sys, queue, time, threading, cv2from tkinter import *from tkinter import fontfrom PIL import Image, ImageTk#GUIclass 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)}")        
查看完整描述

2 回答

?
飲歌長(zhǎng)嘯

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊

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

  • 創(chuàng)建以下實(shí)例botGUIrobotGUI = botGUI()

  • 創(chuàng)建線程并啟動(dòng)它

  • 稱呼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()


查看完整回答
反對(duì) 回復(fù) 2023-10-05
?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

您必須在所有函數(shù)之外定義 robotsGUI,如下所示:


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)


查看完整回答
反對(duì) 回復(fù) 2023-10-05
  • 2 回答
  • 0 關(guān)注
  • 130 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)