kein problem mehr mit klassen .. codeupdate

Fragen zu Tkinter.
powerslide
User
Beiträge: 51
Registriert: Freitag 10. Dezember 2004, 09:05
Wohnort: Erlangen
Kontaktdaten:

okay.. dann bestel ich das gui zeug mal mit rein.. danach nerv ich euch nomma :)

schonmal danke für die hilfe!



hm.. schaut doch schon ganz gut aus.. bissl viele self's :P

Code: Alles auswählen

#---------------------------------------------------------
# imports

import os
import string
import re
import tkFileDialog
import ScrolledText
from Tkinter import *

class convertWindow:

#---------------------------------------------------------
# build gui

    def __init__(self):
        
        self.root=Tk()

        frame=Frame(self.root)
        frame.pack(expand=YES)

        self.root.title('XML converter vol 1.0')

        selectrahmen = Frame(self.root, relief="groove", borderwidth="2")
        selectrahmen.pack(fill="x")

        select1 = Frame(selectrahmen)
        select1.pack(fill="x")
        select2 = Frame(selectrahmen)
        select2.pack(fill="x")

        selectLab = Label(select1, text='Would you like to convert a single file or multiple files?')
        selectLab.pack(side = "top", anchor="w")

        self.v = IntVar()
        Radiobutton(select2, text="single", variable=self.v, value=1, command=lambda: self.sing()).pack(side="left")
        Radiobutton(select2, text="multiple", variable=self.v, value=2, command=lambda: self.mult()).pack(side="left")
        self.v.set('1')
        
        eingaberahmen = Frame(self.root, relief="groove", borderwidth="2")
        eingaberahmen.pack(fill="x")

        suchDatei = Label(eingaberahmen, text = 'File: ')
        suchDatei.grid(row=1,column=1)

        self.datei = Entry(eingaberahmen, width=45)
        self.datei.grid(row=1,column=2)

        self.dat = Button(eingaberahmen, text = 'search', command=lambda: self.get_file())
        self.dat.grid(row=1,column=3)

        suchPfad = Label(eingaberahmen, text = 'Directory: ')
        suchPfad.grid(row=2,column=1)

        self.path = Entry(eingaberahmen, width=45, state='disabled')
        self.path.grid(row=2,column=2)

        self.direct = Button(eingaberahmen, text = 'search',state='disabled', command=lambda: self.get_dir())
        self.direct.grid(row=2,column=3)

        eingaberahmen2 = Frame(self.root)
        eingaberahmen2.pack()

        self.cancel = Button(eingaberahmen2, text = 'Cancel', command=lambda: self.can())
        self.cancel.pack(side='left')
        self.ok = Button(eingaberahmen2, text = 'OK')
        self.ok.pack(side='left')

        ausgaberahmen = Frame(self.root, relief="groove", borderwidth="2")
        ausgaberahmen.pack(fill="x")

        ausgabeLab = Label(ausgaberahmen, text = 'Messagescreen').pack(side = "top", anchor="w")

        self.ausgabe = ScrolledText.ScrolledText(ausgaberahmen, width=61, height=10, state='disabled', bg='white')
        self.ausgabe.pack()
        self.set_out('Please select a file for conversion.', 'y')

#---------------------------------------------------------
# get directory

    def get_dir(self):
        self.path.insert(0,tkFileDialog.askdirectory())

#---------------------------------------------------------
# get filepath

    def get_file(self):
        self.datei.insert(0,tkFileDialog.askopenfilename())

#---------------------------------------------------------
# cancel

    def can(self):
        old1 = self.path["state"]
        old2 = self.datei["state"]
        self.path["state"] = "normal"
        self.datei["state"] = "normal"                 
        self.path.delete(0,END)
        self.datei.delete(0,END)
        self.path["state"] = old1
        self.datei["state"] = old2
        self.v.set('1')
        self.path["state"] = "disabled"
        self.direct["state"] = "disabled"
        self.datei["state"] = "normal"
        self.dat["state"] = "normal"
        self.set_out('Please select a file for conversion.', 'y')

#---------------------------------------------------------
# convert a single file window behaviour
    
    def sing(self):
        self.path.delete(0,END)
        #self.datei.delete(0,END)
        self.path["state"] = "disabled"
        self.direct["state"] = "disabled"
        self.datei["state"] = "normal"
        self.dat["state"] = "normal"
        self.set_out('Please select a file for conversion.', 'y')

#---------------------------------------------------------
# convert multiple files window behaviour
   
    def mult(self):
        #self.path.delete(0,END)
        self.datei.delete(0,END)
        self.path["state"] = "normal"
        self.direct["state"] = "normal"
        self.datei["state"] = "disabled"
        self.dat["state"] = "disabled"
        self.set_out('Please select a directory to make the rogram search for files.\nNote: ALL xml files will be converted!', 'y')

#---------------------------------------------------------
# print messages to messagescreen

    def set_out(self, text, opt):
        text = text + '\n--------------------------------------------------------------------------------------------------------------------------\n'
        self.ausgabe["state"] = "normal"
        if (opt == 'y'):
            self.ausgabe.delete(1.0,END)
        self.ausgabe.insert(END, str(text))
        self.ausgabe["state"] = "disabled"
        self.ausgabe.see(END)

#---------------------------------------------------------
# start gui

convert = convertWindow()
convert.root.mainloop()
weiter gehts wahrscheinlich erst am dienstag.. muss bis montag noch nen vortrag vorbereiten.. bis denne

slide
How many people can read hex if only you and dead people can read hex?

There are 10 types of people in the world: Those who understand binary, and those who don't...
powerslide
User
Beiträge: 51
Registriert: Freitag 10. Dezember 2004, 09:05
Wohnort: Erlangen
Kontaktdaten:

so.. hier der aktuelle stand

Code: Alles auswählen

#---------------------------------------------------------
# imports

import os
import string
import re
import tkFileDialog
import tkMessageBox
import ScrolledText
from Tkinter import *

class convertWindow:

#---------------------------------------------------------
# build gui

    def __init__(self):
        
        self.root=Tk()

        frame=Frame(self.root)
        frame.pack(expand=YES)

        self.root.title('XML converter vol 1.0')

        selectrahmen = Frame(self.root, relief="groove", borderwidth="2")
        selectrahmen.pack(fill="x")

        select1 = Frame(selectrahmen)
        select1.pack(fill="x")
        select2 = Frame(selectrahmen)
        select2.pack(fill="x")

        selectLab = Label(select1, text='Would you like to convert a single file or multiple files?')
        selectLab.pack(side = "top", anchor="w")

        self.v = IntVar()
        Radiobutton(select2, text="single", variable=self.v, value=1, command=self.sing).pack(side="left")
        Radiobutton(select2, text="multiple", variable=self.v, value=2, command=self.mult).pack(side="left")
        self.v.set('1')
        
        eingaberahmen = Frame(self.root, relief="groove", borderwidth="2")
        eingaberahmen.pack(fill="x")

        suchDatei = Label(eingaberahmen, text = 'File: ')
        suchDatei.grid(row=1,column=1)

        self.datei = Entry(eingaberahmen, width=45)
        self.datei.grid(row=1,column=2)

        self.dat = Button(eingaberahmen, text = 'search', command=self.get_file)
        self.dat.grid(row=1,column=3)

        suchPfad = Label(eingaberahmen, text = 'Directory: ')
        suchPfad.grid(row=2,column=1)

        self.path = Entry(eingaberahmen, width=45, state='disabled')
        self.path.grid(row=2,column=2)

        self.direct = Button(eingaberahmen, text = 'search',state='disabled', command=self.get_dir)
        self.direct.grid(row=2,column=3)

        eingaberahmen2 = Frame(self.root)
        eingaberahmen2.pack()

        self.cancel = Button(eingaberahmen2, text = 'Cancel', command=self.can)
        self.cancel.pack(side='left')
        self.ok = Button(eingaberahmen2, text = 'OK', command=self.init)
        self.ok.pack(side='left')

        ausgaberahmen = Frame(self.root, relief="groove", borderwidth="2")
        ausgaberahmen.pack(fill="x")

        ausgabeLab = Label(ausgaberahmen, text = 'Messagescreen').pack(side = "top", anchor="w")

        self.ausgabe = ScrolledText.ScrolledText(ausgaberahmen, width=61, height=10, state='disabled', bg='white')
        self.ausgabe.pack()
        self.set_out('Please select a file for conversion.', 'y')

#---------------------------------------------------------
# get directory

    def get_dir(self):
        self.path.insert(0,tkFileDialog.askdirectory())

#---------------------------------------------------------
# get filepath

    def get_file(self):
        self.datei.insert(0,tkFileDialog.askopenfilename())

#---------------------------------------------------------
# cancel

    def can(self):
        old1 = self.path["state"]
        old2 = self.datei["state"]
        self.path["state"] = "normal"
        self.datei["state"] = "normal"                 
        self.path.delete(0,END)
        self.datei.delete(0,END)
        self.path["state"] = old1
        self.datei["state"] = old2
        self.v.set('1')
        self.path["state"] = "disabled"
        self.direct["state"] = "disabled"
        self.datei["state"] = "normal"
        self.dat["state"] = "normal"
        self.set_out('Please select a file for conversion.', 'y')

#---------------------------------------------------------
# convert a single file window behaviour
    
    def sing(self):
        self.path.delete(0,END)
        #self.datei.delete(0,END)
        self.path["state"] = "disabled"
        self.direct["state"] = "disabled"
        self.datei["state"] = "normal"
        self.dat["state"] = "normal"
        self.set_out('Please select a file for conversion.', 'y')

#---------------------------------------------------------
# convert multiple files window behaviour
   
    def mult(self):
        #self.path.delete(0,END)
        self.datei.delete(0,END)
        self.path["state"] = "normal"
        self.direct["state"] = "normal"
        self.datei["state"] = "disabled"
        self.dat["state"] = "disabled"
        self.set_out('Please select a directory to make the rogram search for files.\nNote: ALL xml files will be converted!', 'y')

#---------------------------------------------------------
# print messages to messagescreen

    def set_out(self, text, opt):
        text = text + '\n--------------------------------------------------------------------------------------------------------------------------\n'
        self.ausgabe["state"] = "normal"
        if (opt == 'y'):
            self.ausgabe.delete(1.0,END)
        self.ausgabe.insert(END, str(text))
        self.ausgabe["state"] = "disabled"
        self.ausgabe.see(END)

#---------------------------------------------------------
# start conversion

    def init(self):
        conversion = conv()

#---------------------------------------------------------
# conversion

class conv:

    def __init__(self):

        x = convert.v.get()
        if (x == 1):
            pfad = convert.datei.get()
            print pfad
            convert.set_out(pfad, 'n')
            if (pfad == ''):
                tkMessageBox.showerror('Error','You must specify a file to convert!')
                return
            self.convertfile(pfad)
            pfad, filename = os.path.split(pfad)
            self.bak_killer_call(pfad)
            self.restart()
        elif (x == 2):
            pfad = convert.path.get()
            convert.set_out(pfad, 'n')
            if (pfad == ''):
                tkMessageBox.showerror('Error','You must specify a directory!')
                return
            self.multipleFiles(pfad, '.xml')
            self.bak_killer_call(pfad)
            self.restart()

#---------------------------------------------------------
# restart program or quit

    def restart(self):
        eingabe = tkMessageBox.askyesno('More...','Would you like to convert more files?')
        if (eingabe == 'True'):
            print 'true'
            return
        elif (eingabe == 'False'):
            print 'false'
            convert.set_out('phasing down application','n')
            convert.root.destroy()

#---------------------------------------------------------    
# filter definitions
    
    def f1(self, text):
        return text.replace("[","").replace("]","")

    def f2(self, text, old, new):
        while 1:
            ort = re.search (old, text)
            if ort == None:
                break
            text = re.sub (old, new, text)
        return text

    def f3(self, text):
        while 1:
            ort = re.search ('(.+?)', text)
            if ort == None:
                break
            text = re.sub('(.+?)', r'\1', text)
        return text     

#---------------------------------------------------------
# multiple file search and convert
  
    def multipleFiles(self, path, extension, depth=0):
        for item in os.listdir(path):
            itemWithPath = os.path.join(path, item)
            if item.endswith(extension):
                datei = itemWithPath
                convert.set_out('Starting conversion', 'n')
                in_file = open(datei,"r")
                inhalt = in_file.read()
                in_file.close()
                bak = datei + '_bak'
                try:
                    bak_file = open(bak, 'w')
                    bak_file.write(inhalt)
                    bak_file.close()
                except IOError:
                    convert.set_out('Backup file could not be generated','n')
                new_string = str(inhalt)
                new_string = string.replace(new_string, '[Q-]', '')
                new_string = string.replace(new_string, '[PM-]', '')
                new_string = string.replace(new_string, '[role]', '')
                #new_string = f1(new_string)
                new_string = self.f3(new_string)
                new_string = self.f2(new_string, '', '')
                new_string = self.f2(new_string, '', '')
                try:
                    out_file = open(datei, "w")    
                except IOError:
                    convert.set_out('File could not be opened!','n')
                out_file.write(new_string)
                out_file.close()
                convert.set_out('File converted and written','n')
            if os.path.isdir(itemWithPath):
                multipleFiles(itemWithPath, extension, depth + 1)

#---------------------------------------------------------
# single file convert

    def convertfile(self, datei):
        convert.set_out('Starting conversion','n')
        ext_match = datei.endswith('.xml')
        if (ext_match == 'False'):
            tkMessageBox.showerror('Error','Invalid File!')
            return
        in_file = open(datei,"r")
        inhalt = in_file.read()
        in_file.close()
        bak = datei + '_bak'
        try:
            bak_file = open(bak, 'w')
            bak_file.write(inhalt)
            bak_file.close()
        except IOError:
            convert.set_out('Backup file could not be generated','n')
        new_string = str(inhalt)
        new_string = string.replace(new_string, '[Q-]', '')
        new_string = string.replace(new_string, '[PM-]', '')
        new_string = string.replace(new_string, '[role]', '')
        new_string = self.f1(new_string)
        new_string = self.f3(new_string)
        new_string = self.f2(new_string, '', '')
        new_string = self.f2(new_string, '', '')
        try:
            out_file = open(datei, "w")    
        except IOError:
            convert.set_out('File could not be opened!','n')
        out_file.write(new_string)
        out_file.close()
        convert.set_out('File converted and written','n')

#---------------------------------------------------------
# bak_killer call

    def bak_killer_call(self, pfad):
        convert.set_out('starting remove engine','n')
        eingabe = tkMessageBox.askyesno('Remove backup?','Would you like to remove the backup [*.xml_bak] files?')
        if (eingabe == 'False'):
            convert.set_out('To remove backup files, simply run bak_remover.','n')
            return
        elif (eingabe == 'True'):
            bak_killer(pfad, '.xml_bak')

#---------------------------------------------------------
# backup killer                

    def bak_killer(self, path, extension, depth=0):
        for item in os.listdir(path):
            itemWithPath = os.path.join(path, item)
            if item.endswith(extension):
                os.remove (itemWithPath)
            if os.path.isdir(itemWithPath):
                bak_killer(itemWithPath, extension, depth + 1)


#---------------------------------------------------------
# start gui

convert = convertWindow()
convert.root.mainloop()




funktioniert .. naja .. halb :P

zumindest bekomm ich langsam ein gefühl für klassen.. und das ist ja schonmal was wert..

was er aber z.b. nicht macht ist das löschen der backup dateien..
sehr komisch das.. die funktion an sich.. ohne gui funktioniert ganz hervorragend.. und beim restart .. ja.. da macht er quasi gar nix.. es kommt zwar die message box .. aber das wars au wieder *grübel*
Zuletzt geändert von powerslide am Dienstag 25. Januar 2005, 16:05, insgesamt 1-mal geändert.
How many people can read hex if only you and dead people can read hex?

There are 10 types of people in the world: Those who understand binary, and those who don't...
mawe
Python-Forum Veteran
Beiträge: 1209
Registriert: Montag 29. September 2003, 17:18
Wohnort: Purkersdorf (bei Wien [Austria])

Hi!

Hab den Code nur mal kurz überflogen, darum auch nur ein kleiner Hinweis:
Wenn Du bei z.B. einem Button als command eine Funktion ohne Argumente aufrufst, brauchst Du kein lambda.

Code: Alles auswählen

self.dat = Button(eingaberahmen, text = 'search', command=self.get_file) 
Gruß, mawe
powerslide
User
Beiträge: 51
Registriert: Freitag 10. Dezember 2004, 09:05
Wohnort: Erlangen
Kontaktdaten:

okay.. also das lambda ist weg..

und ich glaub ich weiss wo der fehler liegt..

bei den askyesno teilen.. vermute ich.. hab aber keine ahnung wieso..
ihr vielleicht?

und irgendwie haut das mit meinem messagewindow (set_out anweisungen) auch nicht hin.. also haut schon hin aber er gibt das immer blockweise aus.. das ist doof!
How many people can read hex if only you and dead people can read hex?

There are 10 types of people in the world: Those who understand binary, and those who don't...
mawe
Python-Forum Veteran
Beiträge: 1209
Registriert: Montag 29. September 2003, 17:18
Wohnort: Purkersdorf (bei Wien [Austria])

Hi!

Ich glaub ich hab's:

Code: Alles auswählen

def bak_killer_call(self, pfad):
        convert.set_out('starting remove engine','n')
        eingabe = tkMessageBox.askyesno('Remove backup?','Would you like to remove the backup [*.xml_bak] files?')
        if not eingabe:      # statt == 'False'
            convert.set_out('To remove backup files, simply run bak_remover.','n')
            return
        else:
            self.bak_killer(pfad, 'xml_bak')    # kein . vor der Endung!  self !
Gruß, mawe
powerslide
User
Beiträge: 51
Registriert: Freitag 10. Dezember 2004, 09:05
Wohnort: Erlangen
Kontaktdaten:

kewl.. werd ich morgen gleich mal probieren.. danke schonmal :D
How many people can read hex if only you and dead people can read hex?

There are 10 types of people in the world: Those who understand binary, and those who don't...
powerslide
User
Beiträge: 51
Registriert: Freitag 10. Dezember 2004, 09:05
Wohnort: Erlangen
Kontaktdaten:

sry for double post.. aber

super cool.. funktioniert .. scheint zumindest so ..

besten dank!
How many people can read hex if only you and dead people can read hex?

There are 10 types of people in the world: Those who understand binary, and those who don't...
Antworten