首页 > 解决方案 > wxpython无法导入同一目录中的文件

问题描述

我想用 wxpython(在 python 3.6.1 中)制作一个简单的 gui,因为我不想再为这个项目使用命令行了。

我在另一个文件中有一个用于逻辑的大文件,其他非 wxpython 文件也使用该文件。

但是当我尝试导入我的逻辑类时,我得到了这个错误:

ImportError:无法导入名称“QuestionAsk”

我使用这条线来导入逻辑类:

from Get import QuestionAsk

文件“Get.py”与 gui 文件位于同一目录中。 这里

但它不起作用为什么以及如何导入这个文件?

顺便说一下,我从 get.py 文件中调用 gui.py 文件并从 Asker.py 文件中导入 get.py 文件。

ps:请尽量不要回答:“只需将该类的内容复制到您的gui文件中”,因为如果没有其他解决方案,我会在其他地方使用这个类,那没关系,但这只是丑陋和低效。

编辑这里是启动 Gui 的 Get 文件中的代码:

def graphical_start():
    app = wx.App(False)  # int app
    frame = GuiVocCard()  # set frame (GuiVocCard is in the main Gui File)
    frame.Show()  # show frame
    app.MainLoop()  # execute loop

这是 Gui 主类“GuiVocCard”的开始:

class GuiVocCard(wx.Frame):

    def __init__(self):

        self.language = "es"
        self.transList = "C:\\Users\\Justus\\Desktop\\Schule\\spa\VocabGeter\\translations\\big_translation.json"
        self.verb_forms = [0,2]
        self.High_Score = 0
        self.s_file = "scores.json"
        self.S_chunk = 40
        self.chunk_file = "chunks.json"

       self.load_config()
       self.q_ask = QuestionAsk(lan=self.language, trans=self.transList, verb_forms=self.verb_forms,
                             scores_file=self.s_file, chunks_file=self.chunk_file, chunk_size=self.S_chunk)
       ...

编辑 2:

QuestionAsk 在 Get.py 文件中定义:

class QuestionAsk:

   def __init__(self,lan="es",trans="C:\\Users\\Justus\\Desktop\\Schule\\spa\VocabGeter\\translations\\translation.json"
             ,verb_forms=[0,2],scores_file="scores.json",chunks_file="chunks.json",chunk_size=40):

       self.language = lan
       self.transList = trans
       self.verb_forms = verb_forms
       self.High_Score = 0
       self.s_file = scores_file
       self.S_chunk = chunk_size
       self.chunk_file = chunks_file

修复它(耶):

我从 Get.py 文件中删除了 graphics_start 函数并将其粘贴到 gui.py 文件中。我现在直接从 Asker.py 文件中导入 Gui.py 文件。

标签: python-3.ximportwxpython

解决方案


推荐阅读