Найти - Пользователи
Полная версия: Помогите: GUI + поиск и добавление нужных строк (проблема с классами)
Начало » Центр помощи » Помогите: GUI + поиск и добавление нужных строк (проблема с классами)
1
vovafreak
from tkMessageBox import *
from Tkinter import *
from tkFileDialog import *
import fileinput

class TMR(Frame):

def __init__(self, parent):
Frame.__init__(self, parent)

self.parent = parent
self.menu_GUI()

def menu_GUI(self):

self.parent.title(“Triple Modular Redundancy”)
self.pack(fill=BOTH, expand=1)

menubar = Menu(self.parent)
self.parent.config(menu=menubar)

filemenu = Menu(menubar)
filemenu.add_command(label=“Open…”, command=self.file_open)
filemenu.add_command(label=“Save as…”, command=self.file_save)
filemenu.add_separator()
filemenu.add_command(label=“Exit”, command=self.close)
menubar.add_cascade(label=“File”, menu=filemenu)

toolmenu = Menu(menubar)
toolmenu.add_command(label=“Handle”, command=self.file_search)
menubar.add_cascade(label=“Tool”, menu=toolmenu)

aboutmenu = Menu(menubar)
aboutmenu.add_command(label=“About”, command=self.about)
menubar.add_cascade(label=“Help”, menu=aboutmenu)

self.txt = Text(self)
self.txt.pack(fill=BOTH, expand=1)

def file_open(self):
file = askopenfile(mode='rb',title='Choose file')
if file != None:
data = file.read()
print data
self.txt.delete(1.0, END)
for i in data:
self.txt.insert(END, i)
return data


“”“def file_read(self, data):

if file != None:
data1 = file.read()
return data1
print ”data1“”“”“

def close(self):
if askyesno(”Exit“, ”Do you want to quit?“):
self.parent.destroy()

def about(self):

showinfo(” “,” This is TMR editor.\nTMR is a fault- tolerant form of N-modular redundancy, in which three systems perform a process \
and that result is processed by a majority-voting system to produce a single output.\
If any one of the three systems fails, the other two systems can correct and mask the fault.“)

def file_save(self):

f = tkFileDialog.asksaveasfile(mode='w', defaultextension=”.py“)
if f is None:
return
text2save = str(self.txt.get(1.0, END))
f.write(text2save)
f.close()

def file_search(self):
text = self.file_read(data1)
print text
for _str in text:
if _str == ”if“:
print ”i find“


def main():
root = Tk()
ex = TMR(root)
root.geometry(”800x600")
root.mainloop()


if __name__ == ‘__main__’:
main()


Собственно, что получилось, но не удается производить поиск по строкам, все время ошибка с классами, помогите, пожалуйста, начать поиск в функции file_search по открытому файлу.
vovafreak
код
terabayt
from tkMessageBox import *
from Tkinter import *
from tkFileDialog import *
import fileinput 
class TMR(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)   
        self.parent = parent
        self.data = ''
        self.menu_GUI()
    def menu_GUI(self):
        self.parent.title("Triple Modular Redundancy")
        self.pack(fill=BOTH, expand=1)
        menubar = Menu(self.parent)
        self.parent.config(menu=menubar)
        filemenu = Menu(menubar)
        filemenu.add_command(label="Open...", command=self.file_open)
        filemenu.add_command(label="Save as...", command=self.file_save)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=self.close)
        menubar.add_cascade(label="File", menu=filemenu)
        toolmenu = Menu(menubar)
        toolmenu.add_command(label="Handle", command=self.file_search)
        menubar.add_cascade(label="Tool", menu=toolmenu)
        aboutmenu = Menu(menubar)
        aboutmenu.add_command(label="About", command=self.about)
        menubar.add_cascade(label="Help", menu=aboutmenu)
        self.txt = Text(self)
        self.txt.pack(fill=BOTH, expand=1)
    def file_open(self):
        self.file = askopenfile(mode='rb',title='Choose file')
        if self.file != None:
            self.data = self.file.readlines()
            print self.data
            self.txt.delete(1.0, END)
            for i in self.data:
                self.txt.insert(END, i)
        return self.data
    def close(self):
        if askyesno("Exit", "Do you want to quit?"):
            self.parent.destroy()
    def about(self):
        showinfo("Vladimir Soloviev","               This is TMR editor.\nTMR is a fault- tolerant form of N-modular redundancy, in which three systems perform a process \
and that result is processed by a majority-voting system to produce a single output.\
If any one of the three systems fails, the other two systems can correct and mask the fault.\
                                    \n(test version) \n MIET \n 2015")
    def file_save(self):
        f = tkFileDialog.asksaveasfile(mode='w', defaultextension=".py")
        if f is None:
            return
        text2save = str(self.txt.get(1.0, END))
        f.write(text2save)
        f.close()
    def file_search(self):
        if not self.data:
            self.file_open()
        if "if\r\n" in self.data:
            print "if find"
        else:
            print "if not find"
def main():
    root = Tk()
    ex = TMR(root)
    root.geometry("800x600")
    root.mainloop()  
if __name__ == '__main__':
    main()
вот, я заставил это работать, но почитайте еще литературы!
4kpt_III
Как подправленное - неплохо. В общем - не айс Согласен с terabayt. Нужно, конечно, почитать.

vovafreak
Классы у Вас организованы неверно. Много лишнего…
terabayt
4kpt_III снова новый профиль?! я так понял, Вы каждые тысячу сообщений создаете новый, а зачем?
4kpt_III
terabayt Непереносимость золотого и желтого
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB