Ich versuche ein Modul, das mit tkinter gemacht ist in einer übergeordneten Datei zu starten. Das tkinter-Programm bindet Grafiken in Buttons ein. Einzel gestartet funktioniert das Test-Modul, mit folgendem Code (vereinfachtes Modell):
Code: Alles auswählen
import os
from tkinter import *
class Feld:
def __init__(self, game, x, y, width, height, images):
self.leer = images[0]
self.bild=images[1]
self.x=x
self.y=y
self.width=width
self.height=height
self.game=game
self.button=Button(self.game, text="", image=self.leer, compound="center",
width=self.width, height=self.height, state=NORMAL,
padx=-1, pady=-1, command=self.press)
self.button.grid(column=x, row=y)
def press(self):
self.button.config(image=self.bild)
class Game(Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.loadImages()
self.createWidgets()
def loadImages(self):
datei = os.path.join(os.path.dirname(__file__), "Images/Leer_96.gif")
leer = PhotoImage(file=datei)
datei = os.path.join(os.path.dirname(__file__), "Images/bild_256.gif")
bild = PhotoImage(file=datei)
self.images=[]
self.images.append(leer)
self.images.append(bild)
def createWidgets(self):
for x in range(4):
for y in range(4):
button=Feld(self, x, y, 40, 40, self.images)
if __name__=="__main__":
root=Tk()
root.title("Sonar")
app=Game(root)
app.mainloop()
root.destroy()
versuche ich nun dieses Programm als Modul zu starten, so kann er offenbar die Grafiken nicht mehr finden. Es erscheint die Fehlermeldung:
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Anaconda3\lib\tkinter\__init__.py", line 2147, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage7" doesn't exist
Ich habe das Modul wie folgt aufgerufen:
Code: Alles auswählen
[
from tkinter import *
import test
def buttonTest():
testGamet=Tk()
testGamet.title("Test")
app=Game(testGamet)
app.mainloop()
testGamet.destroy()
root=Tk()
button=Button(root, text="Test", command=buttonTest)
button.pack()
root.mainloop()
/code]
Kennt jemand diese Verhalten oder was mache ich falsch, dass das Programm als Modul die Grafiken nicht finden kann?