Vielleicht könnt Ihr mir helfen.
Versuche ein Dateiauswahl Dialog für Touchscreens mit Tkinter zu bauen.
wenn ich von tkSimpleDialog.Dialog
ableite und in Body eine CTouchFileSelectBox gride.
bekomme ich immer die Fehlermeldung:
Code: Alles auswählen
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\tools\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\_user\sg\Projekte\Kontrollprofilanalysator\SVMTrainer.py", line 26, in selectProfile
MyDialog(self)
File "C:\tools\Python26\lib\lib-tk\tkSimpleDialog.py", line 68, in __init__
self.wait_visibility() # window needs to be visible for the grab
File "C:\tools\Python26\lib\lib-tk\Tkinter.py", line 412, in wait_visibility
self.tk.call('tkwait', 'visibility', window._w)
TclError: window ".49860856" was deleted before its visibility changed
Code: Alles auswählen
import fnmatch, re, os
import tkSimpleDialog
from Tkinter import *
from PIL import Image as tImage, ImageTk as ImgTk
fntTitle=("Helvetica", 14)
class AutoScrollbar(Scrollbar):
# a scrollbar that hides itself if it's not needed. only
# works if you use the grid geometry manager.
def set(self, lo, hi):
if float(lo) <= 0.0 and float(hi) >= 1.0:
# grid_remove is currently missing from Tkinter!
self.tk.call("grid", "remove", self)
else:
self.grid()
Scrollbar.set(self, lo, hi)
def pack(self, **kw):
raise TclError, "cannot use pack with this widget"
def place(self, **kw):
raise TclError, "cannot use place with this widget"
## Widget anzeige einer Datei
class CFileWidget(Frame):
def __init__(self,parent, textvariable=None,image="./icon/file.png", bg="white", text=None, command=None , width=100, height=3):
Frame.__init__(self, parent, bg=bg)
Imageicon = tImage.open(image)
graphImage = Imageicon.resize((75, 90), resample=3)
graphImage = ImgTk.PhotoImage(Imageicon)
# Imageicon=self.load_icon(image)
self.ilab=Label(self, image=graphImage)
self.ilab.grid(row=0, column=0, sticky=W+E)
self.ilab.image = graphImage
self.path=text
self.open=False
Button(self, text=os.path.basename(text) , anchor=W, bg="white", relief=None,font=fntTitle , command=command, width=width, height=height).grid(row=0, column=1, sticky=W+E)
self.update()
## Widget anzeige eines Verzeichnisses
class CDirectoryWidget(Frame):
def __init__(self,parent, textvariable=None,image="./icon/folder.png", bg="white", text=None, command=None , updatecmd=None, width=100, height=3, listdir=None, sep="\\", exists=None, isdir=None, modus="all"):
Frame.__init__(self, parent)
Imageicon = tImage.open(image)
self.Modus=modus
self.listdir=os.listdir
if(listdir):
self.listdir=listdir
self.sep=sep
self.exists=os.path.exists
self.isdir=os.path.isdir
if(isdir):
self.isdir=isdir
if(exists):
self.exists=exists
graphImage = Imageicon.resize((75, 90), resample=3)
graphImage = ImgTk.PhotoImage(Imageicon)
self.updatecmd=updatecmd
# Imageicon=self.load_icon(image)
self.ilab=Label(self, image=graphImage)
self.row=0
self.col=1
self.command=command
self.inhaltMap={}
self.ilab.grid(row=0, column=0, sticky=W+E)
self.ilab.image = graphImage
self.path=text
self.open=False
test=os.path.basename(text)
if(test==""):
test=text
Button(self, text=test , anchor=W,font=fntTitle , command=self.activate, width=width, height=height).grid(row=0, column=1, sticky=W+E)
self.update()
def activate(self):
if(not self.open):
if self.exists(self.path):
fList=self.listdir(self.path)
fList.sort()
for f in fList:
if(self.exists(self.path+self.sep+f)):
self.row+=1
if(f in self.inhaltMap):
self.inhaltMap[f].grid(row=self.row, column=1, sticky=W+E)
else:
#Hinzufuegen neuer Verzeichnis oder Datei Widgets
if(self.isdir(self.path+self.sep+f)):
self.inhaltMap[f]=CDirectoryWidget(self, text=self.path+self.sep+f, command=self.command, updatecmd=self.uptodate, modus=self.Modus)
self.inhaltMap[f].config(bg="white")
self.inhaltMap[f].grid(row=self.row, column=1, sticky=W+E)
else:
if(self.Modus=="all"):
self.inhaltMap[f]=CFileWidget(self, text=self.path+self.sep+f, command=self.command)
self.inhaltMap[f].config(bg="white")
self.inhaltMap[f].grid(row=self.row, column=1, sticky=W+E)
else:
if(f in self.inhaltMap):
self.inhaltMap.pop(f).destroy()
self.open=True
else:
self.row=0
for f in self.inhaltMap.iterkeys():
self.inhaltMap[f].grid_remove()
self.open=False
if(self.command!=None):
self.command(self.path)
self.uptodate()
def uptodate(self):
self.update()
if(self.updatecmd!=None):
self.updatecmd()
##Scrollbox mit einsetzen von Widgets
class CTouchFileSelectBox(Frame):
def __init__(self, parent, path=None,command=None, width=300, height=300, modus="all"):
self.path=os.getcwd()
if(path):
self.path=path
self.command=command
Frame.__init__(self, parent, width=width, height=height)
self.maxWidth=width
self.maxHeight=height
self.yscrollbar = AutoScrollbar(self)
self.xscrollbar = AutoScrollbar(self, orient=HORIZONTAL)
self.canvas = Canvas(self, yscrollcommand=self.yscrollbar.set , xscrollcommand=self.xscrollbar.set , bg="white", width=width, height=height)
self.canvas.grid(row=0, column=0, sticky=W+N+E+S)
self.yscrollbar.config(command=self.canvas.yview, width=40)
self.xscrollbar.config(command=self.canvas.xview, width=40)
# make the canvas expandable
self.grid_rowconfigure(0, weight=0)
self.grid_columnconfigure(0, weight=1)
#
# create canvas contents
self.frame = Frame(self.canvas, width=400, height=15)
self.frame.config(bg="white")
self.frame.rowconfigure(0, weight=1)
self.frame.columnconfigure(0, weight=1)
self.yscrollbar.grid(row=0, column=1,padx=5, sticky=N+S)
self.xscrollbar.grid(row=1, column=0,pady=5, sticky=E+W)
# self.frame.grid(row=0, column=0)
self.FileBox=CDirectoryWidget(self.frame, text=self.path, command=self.command, updatecmd=self.uptodate, modus=modus)
self.FileBox.config(bg="white")
self.FileBox.grid(row=0, column=0, sticky="nwse")
self.canvas.create_window(0, 0, anchor=NW, window=self.frame)
self.frame.update_idletasks()
def uptodate(self):
self.canvas.config(scrollregion=self.canvas.bbox("all"))
self.frame.update()
self.canvas.update()
##Das eigentliche Dialog
class MyDialog(tkSimpleDialog.Dialog):
def body(self, master):
self.TFSBox=CTouchFileSelectBox(master, "C:\\", width=600, height=300)
self.TFSBox.grid(row=0)
def ok(self, event=None):
self.TFSBox.grid_remove()
self.TFSBox.destroy()
del self.TFSBox
self.withdraw()
self.update_idletasks()
self.apply()
#
self.cancel()
def cancel(self, event=None):
# put focus back to the parent window
#self.parent.focus_set()
self.destroy()
def apply(self):
return self
root=Tk()
d=MyDialog(root)
Danke im vorraus für eure Hilfe
gruß
Twist