你好,我想从另一个.py程序导入一个类
我试图用python -m运行它(来自Stackoverflow的另一个问题)
from back import b
from myscreen import Toplevel1 as t1
from mysqlscript import mysqlfunction
我希望它能显示Tkinter屏幕,但是它给了我这个...
ImportError: cannot import name 'Toplevel1' from 'myscreen' (C:\Users\myname\Desktop\work\myscreen.py)
Tkinter屏幕:
self.Label1 = tk.Label(top)
self.Label1.place(relx=0.167, rely=0.044, height=21, width=394)
self.Label1.configure(background="#d9d9d9")
self.Label1.configure(disabledforeground="#a3a3a3")
self.Label1.configure(foreground="#000000")
self.Label1.configure(text='''Optionen:''')
self.Label1.configure(width=394)
self.Button1 = tk.Button(top)
self.Button1.place(relx=0.383, rely=0.133, height=24, width=157)
self.Button1.configure(activebackground="#ececec")
self.Button1.configure(activeforeground="#000000")
self.Button1.configure(background="#d9d9d9")
self.Button1.configure(disabledforeground="#a3a3a3")
self.Button1.configure(foreground="#000000")
self.Button1.configure(highlightbackground="#d9d9d9")
self.Button1.configure(highlightcolor="black")
self.Button1.configure(pady="0")
self.Button1.configure(text='''Notfall''')
self.Button1.configure(width=157)
self.Button1.configure(command= b(self = None, a1 = Toplevel1))
self.Button1_1 = tk.Button(top)
self.Button1_1.place(relx=0.383, rely=0.2, height=24, width=157)
self.Button1_1.configure(activebackground="#ececec")
self.Button1_1.configure(activeforeground="#000000")
self.Button1_1.configure(background="#d9d9d9")
self.Button1_1.configure(disabledforeground="#a3a3a3")
self.Button1_1.configure(foreground="#000000")
self.Button1_1.configure(highlightbackground="#d9d9d9")
self.Button1_1.configure(highlightcolor="black")
self.Button1_1.configure(pady="0")
self.Button1_1.configure(text='''A Big option to chose''')
self.Button1_2 = tk.Button(top)
self.Button1_2.place(relx=0.383, rely=0.267, height=24, width=157)
self.Button1_2.configure(activebackground="#ececec")
self.Button1_2.configure(activeforeground="#000000")
self.Button1_2.configure(background="#d9d9d9")
self.Button1_2.configure(disabledforeground="#a3a3a3")
self.Button1_2.configure(foreground="#000000")
self.Button1_2.configure(highlightbackground="#d9d9d9")
self.Button1_2.configure(highlightcolor="black")
self.Button1_2.configure(pady="0")
self.Button1_2.configure(text='''ammm....this is a option''')
self.Button1_3 = tk.Button(top)
self.Button1_3.place(relx=0.017, rely=0.911, height=24, width=127)
self.Button1_3.configure(activebackground="#ececec")
self.Button1_3.configure(activeforeground="#000000")
self.Button1_3.configure(background="#d9d9d9")
self.Button1_3.configure(disabledforeground="#a3a3a3")
self.Button1_3.configure(foreground="#000000")
self.Button1_3.configure(highlightbackground="#d9d9d9")
self.Button1_3.configure(highlightcolor="black")
self.Button1_3.configure(pady="0")
self.Button1_3.configure(text='''back''')
self.Button1_3.configure(width=127)
self.Button1_4 = tk.Button(top)
self.Button1_4.place(relx=0.383, rely=0.4, height=24, width=157)
self.Button1_4.configure(activebackground="#ececec")
self.Button1_4.configure(activeforeground="#000000")
self.Button1_4.configure(background="#d9d9d9")
self.Button1_4.configure(disabledforeground="#a3a3a3")
self.Button1_4.configure(foreground="#000000")
self.Button1_4.configure(highlightbackground="#d9d9d9")
self.Button1_4.configure(highlightcolor="black")
self.Button1_4.configure(pady="0")
self.Button1_4.configure(text='''another option''')
self.Button1_5 = tk.Button(top)
self.Button1_5.place(relx=0.383, rely=0.333, height=24, width=157)
self.Button1_5.configure(activebackground="#ececec")
self.Button1_5.configure(activeforeground="#000000")
self.Button1_5.configure(background="#d9d9d9")
self.Button1_5.configure(disabledforeground="#a3a3a3")
self.Button1_5.configure(foreground="#000000")
self.Button1_5.configure(highlightbackground="#d9d9d9")
self.Button1_5.configure(highlightcolor="black")
self.Button1_5.configure(pady="0")
self.Button1_5.configure(text='''one option''')
它一直在给我那个错误。
所以我试图从另一个文件导入一个类。
答案 0 :(得分:0)
确保C:\Users\myname\Desktop\work
出现在您的PYTHONPATH
环境变量中。
例如,在cygwin bash中使用:
$ cd work
$ export PYTHONPATH=.
$ python foo.py
要进行详细的调试,请import sys
,然后检查sys.path
。
此外,首先使import myscreen
成功的工作也比较简单。
例如,如果您pip install
包装了一个提供myscreen
模块的包装
但缺少Toplevel1
符号,较简单的import
可以揭示这一点。