-1

it's my first time posting on the site. It's definitely early days for me when it comes to learning Python & PyQt5 so I am likely missing something basic. I am making a program that will have some basic functionality but for now I just want to get the button spawning to work.

Here is the code I have so far:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
import sys
import os

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(808, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.alltabswidget = QtWidgets.QTabWidget(self.centralwidget)
        self.alltabswidget.setMinimumSize(QtCore.QSize(790, 541))
        self.alltabswidget.setMaximumSize(QtCore.QSize(791, 561))
        self.alltabswidget.setObjectName("alltabswidget")
        self.gaming = QtWidgets.QWidget()
        self.gaming.setObjectName("gaming")
        self.label = QtWidgets.QLabel(self.gaming)
        self.label.setGeometry(QtCore.QRect(6, 2, 781, 511))
        self.label.setText("")
        self.label.setPixmap(QtGui.QPixmap("../../../../../Pictures/Coding Images/Coding.jpg"))
        self.label.setScaledContents(True)
        self.label.setObjectName("label")
        self.firstcodingbutton = QtWidgets.QPushButton(self.gaming)
        self.firstcodingbutton.setGeometry(QtCore.QRect(50, 30, 141, 51))
        self.firstcodingbutton.setObjectName("firstcodingbutton")
        self.alltabswidget.addTab(self.gaming, "")
        self.gaming1 = QtWidgets.QWidget()
        self.gaming1.setObjectName("gaming1")
        self.label_2 = QtWidgets.QLabel(self.gaming1)
        self.label_2.setGeometry(QtCore.QRect(6, 2, 781, 511))
        self.label_2.setText("")
        self.label_2.setPixmap(QtGui.QPixmap("../../../../../Pictures/Coding Images/Gaming.jpg"))
        self.label_2.setScaledContents(True)
        self.label_2.setObjectName("label_2")
        self.firstgamingbutton = QtWidgets.QPushButton(self.gaming1)
        self.firstgamingbutton.setGeometry(QtCore.QRect(50, 30, 141, 51))
        self.firstgamingbutton.setObjectName("firstgamingbutton")
        self.alltabswidget.addTab(self.gaming1, "")
        self.fileaccess = QtWidgets.QWidget()
        self.fileaccess.setObjectName("fileaccess")
        self.label_3 = QtWidgets.QLabel(self.fileaccess)
        self.label_3.setGeometry(QtCore.QRect(6, 2, 781, 511))
        self.label_3.setText("")
        self.label_3.setPixmap(QtGui.QPixmap("../../../../../Pictures/Coding Images/Other.jpg"))
        self.label_3.setScaledContents(True)
        self.label_3.setObjectName("label_3")
        self.firstfileacessbutton = QtWidgets.QPushButton(self.fileaccess)
        self.firstfileacessbutton.setGeometry(QtCore.QRect(50, 30, 141, 51))
        self.firstfileacessbutton.setObjectName("firstfileacessbutton")
        self.alltabswidget.addTab(self.fileaccess, "")
        self.gridLayout.addWidget(self.alltabswidget, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 808, 21))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionnewshortcut = QtWidgets.QAction(MainWindow)
        self.actionnewshortcut.setObjectName("actionnewshortcut")
##        self.actionnewshortcut.triggered.connect(clicknewshortcut)
        self.actionsave = QtWidgets.QAction(MainWindow)
        self.actionsave.setObjectName("actionsave")
        self.actionexit = QtWidgets.QAction(MainWindow)
        self.actionexit.setObjectName("actionexit")
        self.menuFile.addAction(self.actionnewshortcut)
        self.menuFile.addAction(self.actionsave)
        self.menuFile.addAction(self.actionexit)
        self.menubar.addAction(self.menuFile.menuAction())

        self.retranslateUi(MainWindow)
        self.alltabswidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Shortcuts"))
        self.firstcodingbutton.setText(_translate("MainWindow", "Rename Me"))
        self.alltabswidget.setTabText(self.alltabswidget.indexOf(self.gaming), _translate("MainWindow", "Coding"))
        self.firstgamingbutton.setText(_translate("MainWindow", "Rename Me"))
        self.alltabswidget.setTabText(self.alltabswidget.indexOf(self.gaming1), _translate("MainWindow", "Gaming"))
        self.firstfileacessbutton.setText(_translate("MainWindow", "Rename Me"))
        self.alltabswidget.setTabText(self.alltabswidget.indexOf(self.fileaccess), _translate("MainWindow", "File Access"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.actionnewshortcut.setText(_translate("MainWindow", "New Shortcut"))
        self.actionnewshortcut.setShortcut(_translate("MainWindow", "Ctrl+N"))
        self.actionsave.setText(_translate("MainWindow", "Save"))
        self.actionsave.setShortcut(_translate("MainWindow", "Ctrl+S"))
        self.actionexit.setText(_translate("MainWindow", "Exit"))
        self.actionexit.setShortcut(_translate("MainWindow", "Ctrl+E"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

I did find this question already on the site but the main window is setup a bit differently in that example and I'm not sure how to insert the button generating function into my code. Essentially I want to have it so when you go to file, then new shortcut, it generates a button just below the first one on the current selected tab. I can mess with the geometry afterwards but first I'm trying to get the generation to work. I have the triggered signal commented out for now since it's not linked to anything yet.

Moha369
  • 815
  • 7
  • 16
Dan
  • 3
  • 1
  • 1
    The window is setup differently and you're having difficulties because you're trying to modify a pyuic generated file, which is something that should **never** be done. To correctly use those files (which you should now generate again) follow the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). – musicamante Oct 04 '20 at 06:21
  • I'm not sure what you mean in this comment. I understand that you should complete your ui design in pyuic5 and (I believe) I've done as much as I can in there before converting it to a .py file. What I have written so far and shared in my post works properly (accesses the image files for the background of the 3 tabs). I am not trying to edit something with those files, I am trying to have buttons generate upon clicking an option in the file menu. I had someone else respond with a function that will hopefully resolve things, I will update after testing. – Dan Oct 05 '20 at 19:49
  • I'm not saying that it doesn't work, but that you shouldn't do it in that way. Files generated by pyuic should never be modified; one of the most important reasons is because at some point you might need to modify your UI and so generate again the py file with pyuic, making it very hard to integrate with existing code you've written so far. Those files should be imported instead in another "main" script instead, which will contain the "logic" of your program. In that way, you will be able to eventually modify your UI with ease and the only thing left is to generate the file again. – musicamante Oct 05 '20 at 21:16
  • What your saying makes sense. But since I'm newer to this I don't know how to structure the logic separately from the .py file that was made from the PyQT Designer output, I will have to do more reading. – Dan Oct 08 '20 at 04:09

1 Answers1

1

Add self to the slot

self.actionnewshortcut.triggered.connect(self.clicknewshortcut)

Define the method in the class

def clicknewshortcut(self):
    tab = self.alltabswidget.currentWidget()
    first_btn = tab.findChild(QPushButton)
    btn = QPushButton('New Button', tab)
    btn.setGeometry(first_btn.geometry().translated(0, first_btn.height()))
    btn.show()

It gets the current widget, creates a new button, and sets its geometry below the first button in that widget.

alec
  • 5,799
  • 1
  • 7
  • 20