【发布时间】:2019-01-31 14:07:31
【问题描述】:
因为我的 python 脚本变得很大,所以我将它拆分为多个文件。 然后我将needet文件导入到我的主文件中。
然后我想从包含的文件中打开一个在主文件中定义的类。但它总是告诉我主文件中定义的类没有定义。
这里是主文件:
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
# page classes import
from travel import PageTravel
from contact import PageContact
from dangers import PageDangers
#Main Window wird gestartet.
class PTools(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
#self.geometry("%dx%d"%(self.winfo_screenwidth()-200,self.winfo_screenheight()-200))
self._frame = None
self.switch_frame(StartPage)
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack(anchor='center')
class StartPage(ttk.Frame):
def __init__(self, master):
s=ttk.Style()
s.theme_use('winnative')
ttk.Frame.__init__(self, master)
#Frames werden geöffnet.
alles=ttk.Frame(self)
alles.pack(padx=100)
version=ttk.Frame(self)
version.pack(anchor='e')
self.button1 =ttk.Button(alles, text ="Test1",command=lambda: master.switch_frame(PageTravel),width=50) #command linked
self.button1.pack()
self.button2=ttk.Button(alles, text ="Test2",command=lambda: master.switch_frame(PageDangers),width=50)
self.button2.pack()
self.button4=ttk.Button(alles, text ="Test3",command=lambda: master.switch_frame(PageContact),width=50)
self.button4.pack()
if __name__ == "__main__":
app = PTools()
app.mainloop()
如您所见,我导入了 PageTravel、PageContact 和 PageDanger。同样在这个 ffine 中,我定义了 StartPage 类。这是按下按钮时调用的 PageContact:
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
class PageContact(ttk.Frame):
def __init__(self, master):
ttk.Frame.__init__(self, master)
#label = ttk.Label(self, text="Fragen, Anregungen, Bugs?\n Per Mail bin ich unter ")
#label.pack(side="top", fill="x", pady=10)
button = ttk.Button(self, text="Back to Main Menu",
command=lambda: master.switch_frame(StartPage))
button.pack()
现在这是一个完全不同的文件,我可以调用它,因为我导入了它。然后我尝试通过再次调用“StartPage”返回 MainPage。但在这里它告诉我起始页是未知的。
当我尝试使用
导入起始页时from mainfile import StartPage
它不起作用。
不带行的回溯:
Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):
文件 "C:\Users\Odatas\AppData\Local\Continuum\anaconda3\lib\tkinter__init__.py", 第 1705 行,在 调用 返回 self.func(*args) 文件“C:\Users\Odatas\Documents\Python Scripts\contact.py”,第 36 行,在 command=lambda: master.switch_frame(StartPage)) NameError: name 'StartPage' is not defined
回溯行:
File "<ipython-input-18-2db7e63f920e>", line 1, in <module> runfile('C:/Users/Odatas/Documents/Python Scripts/patrickstools2.py', wdir='C:/Users/Odatas/Documents/Python Scripts') File "C:\Users\Odatas\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile execfile(filename, namespace) File "C:\Users\Odatas\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/Odatas/Documents/Python Scripts/patrickstools2.py", line 23, in <module> from contact import PageContact File "C:\Users\Odatas\Documents\Python Scripts\contact.py", line 20, in <module> from patrickstools2.py import StartPage File "C:\Users\Odatas\Documents\Python Scripts\patrickstools2.py", line 23, in <module> from contact import PageContact ImportError: cannot import name 'PageContact' from 'contact' (C:\Users\Odatas\Documents\Python Scripts\contact.py
【问题讨论】:
-
错误是
from contact import PageContact。目前尚不清楚代码的哪一部分是哪个文件。请edit您的问题并添加 *.py 文件名。 哪个是patrickstools2.py。 -
我无法再编辑我的问题,它总是告诉我我的代码格式错误。第一个代码 sn -p 是 patrickstools2.py 第二个是contact.py
标签: python python-3.x tkinter