3

I am trying to add tabs to my program but it isn't working so far; the tabs show up but over the menubar and I am not sure why. Here is my code:

#! /usr/bin/python

import sys
import os
from 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 a 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 am trying to get it so the tab contains the text box. Any help would be great.

1 Answer 1

3

Don't create a text widget in the initUI, but create it in the newFile and openFile methods and then add it to the tab_widget (also save the reference to the tab_wiget in initUI so you can access it later). For example:

def openFile(self):
    filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', os.getenv('HOME'))[0]
    with open(filename, 'r') as 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))
4
  • It isn't working for me yet; I tried doing it to newFile, but part of the tab box, it looks like, is covering the menu bar still. Here is my code. Commented Aug 11, 2013 at 16:08
  • You are missing self.setCentralWidget(self.tab_widget) after line 41. Commented Aug 11, 2013 at 19:24
  • Ok, it is almost working now, but for some reason the text area doesn't fill the whole tab area. How would I do that? Here is my current code. Commented Aug 11, 2013 at 19:30
  • Don't create a widget, just add the text directly to the tab_widget. Replace the code in newFile with: text = QtGui.QTextEdit(self.tab_widget); self.tab_widget.addTab(text, "Untitled.asm") Commented Aug 11, 2013 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.