微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

类 ShareGUI 未定义

如何解决类 ShareGUI 未定义

我最近在我的代码中遇到了一个问题 - 它无法识别我导入的类...你能帮帮我吗??我基本上只是做了一些课程来使我的 gui 工作流程更有效率。例如,ButtonFrame 2 只是一个获取大小、静态标题和 2 个按钮标签并制作 gui 框架的类。

from ButtonFrame2 import *
from ShareGUI import *


class ChooseShareGUI(ButtonFrame2):
    """
    ask to have permission to take files from other
    users.
    """
    def __init__(self,username,client):
        """
        :param e: event handler
        """
        super().__init__(None,HOME_PAGE_TITLE,HOME_PAGE_TITLE + " - " + username,ASK_FOR_SHARE_BTN,SHARE_BTN,SYstem_REGISTER_PANEL_SIZE,client)
        self.btn2.Bind(wx.EVT_BUTTON,self.on_share)

    def on_share(self,e):
        """
        :param e: event handler
        :return: shows list of my files to share
        """
        self.Close()
        ShareGUI(self.client)

那么,这是 ShareGUI 类:

from ChooseUserGUI import *
from GeneralGUI import *
from ReadRegistry import *
import os


class ShareGUI(GeneralGUI):
    """
    opens a window with directory dialog and a Next button
    """
    def __init__(self,SHARE_TITLE,INIT_CLOUD_GUI_SIZE,client)
        self.file_to_share = None
        self.static_txt = wx.StaticText(self.pnl,label=CHOOSE_FILE_TO_SHARE)
        self.next_btn = wx.Button(self.pnl,label=NEXT_BTN)
        self.next_btn.Bind(wx.EVT_BUTTON,self.on_next)
        self.browser = wx.FilePickerCtrl()
        self.browser.Create(self.pnl,path=self.client.cloud,pos=wx.DefaultPosition,size=wx.DefaultSize,style=wx.DIRP_DEFAULT_STYLE,name=wx.DirPickerCtrlNameStr)
        self.position()
        self.Show()

    def position(self):
        """
        :return: positions everything nicely
        """
        self.sizer.Add(window=self.static_txt,proportion=PROPORTION,flag=wx.ALL | wx.CENTER,border=BORDER_SMALL)
        self.sizer.Add(window=self.browser,border=BORDER_LARGE)
        self.sizer.Add(window=self.next_btn,border=BORDER_SMALL)
        self.SetSizer(self.sizer)

当我运行我的代码时,错误是:

line 38,in on_share
ShareGUI(self.client)
NameError: name 'ShareGUI' is not defined
    

求求你我真的不知道该怎么办!!!

解决方法

您的导入有问题,您显示的代码不完整。我总是做一个“最小可行的例子”来将问题归结为本质。您向我们展示的内容令人不悦(野生/加星标导入),但它确实有效(如果您的不完整样本被分解为可能的样子)。

最小可行示例,在 main.py 中打印“hello”:

from ShareGUI import *
ShareGUI()

并且在同一个文件夹中/在 ShareGUI.py 上有一个 PYTHONPATH

class ShareGUI:
    def __init__(self):
        print('hello')

你可能在第二次尝试时做了什么:

import ShareGUI    
# …
ShareGUI() # -> TypeError,why do you call a module?
ShareGUI.ShareGUI() # works
,

关于导入以及如何访问您感兴趣的类中的对象,您需要回到绘图板。
探索您有权访问的内容的一个有用工具是 dir()

假设您要导入的类如下:

class ShareGUI:
    def __init__(self):
        print('hello')
        self.client = "Myclient is alive"

从命令行使用 python 解释器:-

>>> from ShareGUI import *
>>> dir(ShareGUI)
['__class__','__delattr__','__dict__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__gt__','__hash__','__init__','__init_subclass__','__le__','__lt__','__module__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__','__weakref__']
>>> dir(ShareGUI())
hello
['__class__','__weakref__','client']
>>> ShareGUI().client
hello
'Myclient is alive'

但是,如果我们更改 import,您将得到不同的结果:-

>>> import ShareGUI
>>> dir(ShareGUI)
['ShareGUI','__builtins__','__cached__','__file__','__loader__','__name__','__package__','__spec__']
>>> dir(ShareGUI.ShareGUI)
['__class__','__weakref__']
>>> dir(ShareGUI.ShareGUI())
hello
['__class__','client']
>>> ShareGUI.ShareGUI().client
hello
'Myclient is alive'

如您所见,您如何执行 import,直接影响您访问导入对象及其组件的方式。
您选择如何导入和访问由您决定,但它们必须一致。

注意参考您的一个评论,self.client 声明客户端是 ShareGUI 中的一个项目,从该类之外引用它,您需要删除 self(您不在 {{1} } 班级)。您必须将其称为组件 ShareGUI of,因此它只是 ShareGUI

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