python - PyQt: Adding tabs -
i trying add tabs program isn't working far; tabs show on menubar , not sure why. here code:
#! /usr/bin/python import sys import os pyqt4 import qtgui class notepad(qtgui.qmainwindow): def __init__(self): super(notepad, self).__init__() self.initui() def initui(self): newaction = qtgui.qaction('new', self) newaction.setshortcut('ctrl+n') newaction.setstatustip('create new file') newaction.triggered.connect(self.newfile) saveaction = qtgui.qaction('save', self) saveaction.setshortcut('ctrl+s') saveaction.setstatustip('save current file') saveaction.triggered.connect(self.savefile) openaction = qtgui.qaction('open', self) openaction.setshortcut('ctrl+o') openaction.setstatustip('open file') openaction.triggered.connect(self.openfile) closeaction = qtgui.qaction('close', self) closeaction.setshortcut('ctrl+q') closeaction.setstatustip('close notepad') closeaction.triggered.connect(self.close) menubar = self.menubar() filemenu = menubar.addmenu('&file') filemenu.addaction(newaction) filemenu.addaction(saveaction) filemenu.addaction(openaction) filemenu.addaction(closeaction) tab_widget = qtgui.qtabwidget(self) # add tab tab1 = qtgui.qwidget() tab_widget.addtab(tab1, "main") self.text = qtgui.qtextedit(tab_widget) self.setcentralwidget(self.text) self.setgeometry(300,300,300,300) self.setwindowtitle('notepad') self.show() def newfile(self): self.text.clear() def savefile(self): filename = qtgui.qfiledialog.getsavefilename(self, 'save file', os.getenv('home')) f = open(filename, 'w') filedata = self.text.toplaintext() f.write(filedata) f.close() def openfile(self): filename = qtgui.qfiledialog.getopenfilename(self, 'open file', os.getenv('home')) f = open(filename, 'r') filedata = f.read() self.text.settext(filedata) f.close() def main(): app = qtgui.qapplication(sys.argv) notepad = notepad() sys.exit(app.exec_()) if __name__ == '__main__': main()
i trying tab contains text box. great.
don't create text widget in initui
, create in newfile
, openfile
methods , add tab_widget
(also save reference tab_wiget
in initui
can access later). example:
def openfile(self): filename = qtgui.qfiledialog.getopenfilename(self, 'open file', os.getenv('home'))[0] open(filename, 'r') f: filedata = f.read() text_widget = qtgui.qtextedit(self.tab_widget) text_widget.settext(filedata) self.tab_widget.addtab(text_widget, os.path.basename(filename))
Comments
Post a Comment