StackedWidget, fails to show interface.ui

1

I have a problem, I try to use a stackedwidget to change the contents of my main window but not the one in the taskbar.

this is my code:

from PyQt5.QtWidgets import QMainWindow,QApplication,QMenu
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtCore import Qt,QPoint
from PyQt5 import uic
from functools import partial



class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("1.ui",self)



        self.stacked = QtWidgets.QStackedWidget()
        self.setCentralWidget(self.stacked)


        self.stacked.addWidget(Widget1())
        #self.stacked.addWidget(Widget2())

        self.Inicio.clicked.connect(partial(self.stacked.setCurrentIndex,0))

class Widget1(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget1, self).__init__(parent)
        self.setLayout(QtWidgets.QVBoxLayout())
        self.layout().addWidget(QtWidgets.QLabel("Primera\nVentana"))


App = QApplication([])
Main = MainWindow()
Main.show()
App.exec_()

this is the file.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>884</width>
    <height>579</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Exe - Ventas</string>
  </property>
  <property name="windowIcon">
   <iconset>
    <normaloff>exe.png</normaloff>exe.png</iconset>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QFrame" name="frame">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>5000</width>
      <height>27</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">border-top:1px solid black;

background:qlineargradient(spread:pad, x1:0.384, y1:1, x2:1, y2:0, stop:0 rgba(0, 0, 0, 127), stop:1 rgba(255, 255, 255, 153));</string>
    </property>
    <property name="frameShape">
     <enum>QFrame::StyledPanel</enum>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Raised</enum>
    </property>
    <widget class="QPushButton" name="Inicio">
     <property name="geometry">
      <rect>
       <x>2</x>
       <y>2</y>
       <width>75</width>
       <height>23</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">QPushButton#Inicio{
border:0px;
border-right:2px solid #00cc00;
background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 0));
}

QPushButton#Inicio:hover{

border-right:2px solid blue;
background:qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(0, 0, 0, 93), stop:1 rgba(255, 255, 255, 153));
}</string>
     </property>
     <property name="text">
      <string>Inicio</string>
     </property>
    </widget>
   </widget>
   <widget class="QPushButton" name="Ventas">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>2</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">QPushButton#Ventas{
border:0px;
border-right:2px solid #00cc00;


}

QPushButton#Ventas:hover{

border-right:2px solid blue;
background:qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(0, 0, 0, 93), stop:1 rgba(255, 255, 255, 153));
}</string>
    </property>
    <property name="text">
     <string>Ventas</string>
    </property>
   </widget>
   <widget class="QPushButton" name="Consulta">
    <property name="geometry">
     <rect>
      <x>240</x>
      <y>2</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">QPushButton#Consulta{
border:0px;
border-right:2px solid #00cc00;


}

QPushButton#Consulta:hover{

border-right:2px solid blue;
background:qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(0, 0, 0, 93), stop:1 rgba(255, 255, 255, 153));
}</string>
    </property>
    <property name="text">
     <string>Consulta</string>
    </property>
   </widget>
   <widget class="QPushButton" name="Mensajeria">
    <property name="geometry">
     <rect>
      <x>320</x>
      <y>2</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">QPushButton#Mensajeria{
border:0px;
border-right:2px solid #00cc00;


}

QPushButton#Mensajeria:hover{

border-right:2px solid blue;
background:qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(0, 0, 0, 93), stop:1 rgba(255, 255, 255, 153));
}</string>
    </property>
    <property name="text">
     <string>Mensajeria</string>
    </property>
   </widget>
   <widget class="QPushButton" name="Registro">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>2</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">QPushButton#Registro{
border:0px;
border-right:2px solid #00cc00;


}

QPushButton#Registro:hover{

border-right:2px solid blue;
background:qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(0, 0, 0, 93), stop:1 rgba(255, 255, 255, 153));
}</string>
    </property>
    <property name="text">
     <string>Registro</string>
    </property>
   </widget>
   <widget class="QPushButton" name="Salir">
    <property name="geometry">
     <rect>
      <x>400</x>
      <y>2</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">QPushButton#Salir{
border:0px;
border-right:2px solid #00cc00;


}

QPushButton#Salir:hover{

border-right:2px solid blue;
background:qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(0, 0, 0, 93), stop:1 rgba(255, 255, 255, 153));
}</string>
    </property>
    <property name="text">
     <string>Salir</string>
    </property>
   </widget>
   <widget class="QFrame" name="frame_2">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>40</y>
      <width>311</width>
      <height>181</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">background:red;</string>
    </property>
    <property name="frameShape">
     <enum>QFrame::StyledPanel</enum>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Raised</enum>
    </property>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

this is the result without the implementation of the stackedWidget this is the result I hope to obtain but using the stacked to only modify what appears below the toolbar that you create

and this is the result I get by implementing the stackedwidget:

I hope you can help me

    
asked by Revsky01 10.07.2018 в 23:26
source

1 answer

2

In my previous answer I have assumed that Home, Sales, etc. were part of the QMenuBar, in your case not you are using it so the strategy must be modified.

First of all you must use layouts to establish elements since they handle the position and the size, in your case modify your .ui

As noted, I have set the buttons within the QFrame through QHBoxLayout, and I have also set the QFrame together with the QStackedWidget through QVBoxlayout, and that QVBoxLayout is part of the centralwidget:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <property name="styleSheet">
   <string notr="true"/>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <property name="leftMargin">
     <number>0</number>
    </property>
    <property name="topMargin">
     <number>0</number>
    </property>
    <property name="rightMargin">
     <number>0</number>
    </property>
    <property name="bottomMargin">
     <number>0</number>
    </property>
    <item>
     <widget class="QFrame" name="topframe">
      <property name="styleSheet">
       <string notr="true">QFrame#topframe{
    background:qlineargradient(spread:pad, x1:0.384, y1:1, x2:1, y2:0, stop:0 rgba(0, 0, 0, 127), stop:1 rgba(255, 255, 255, 15))
}

QFrame#topframe &gt; QPushButton {
    border:0px;
    border-right:2px solid #00cc00;
}

QFrame#topframe &gt; QPushButton:hover {
    border-right:2px solid blue;
    background:qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(0, 0, 0, 93), stop:1 rgba(255, 255, 255, 153));
}</string>
      </property>
      <property name="frameShape">
       <enum>QFrame::StyledPanel</enum>
      </property>
      <property name="frameShadow">
       <enum>QFrame::Raised</enum>
      </property>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <property name="leftMargin">
        <number>2</number>
       </property>
       <property name="topMargin">
        <number>2</number>
       </property>
       <property name="rightMargin">
        <number>0</number>
       </property>
       <property name="bottomMargin">
        <number>2</number>
       </property>
       <item>
        <widget class="QPushButton" name="Inicio">
         <property name="styleSheet">
          <string notr="true"/>
         </property>
         <property name="text">
          <string>Inicio</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="Ventas">
         <property name="text">
          <string>Ventas</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="Registro">
         <property name="text">
          <string>Registro</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="Consulta">
         <property name="text">
          <string>Consulta</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="Mensajeria">
         <property name="text">
          <string>Mensajeria</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="Salir">
         <property name="text">
          <string>Salir</string>
         </property>
        </widget>
       </item>
       <item>
        <spacer name="horizontalSpacer">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>275</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
      </layout>
     </widget>
    </item>
    <item>
     <widget class="QStackedWidget" name="stackedWidget"/>
    </item>
   </layout>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Now it's easy to add the widgets:

from PyQt5 import QtCore, QtWidgets, uic

from functools import partial


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        uic.loadUi("1.ui",self)

        d = {self.Inicio: InicioWidget(), self.Ventas: VentasWidget()}

        for button, window in d.items():
            self.stackedWidget.addWidget(window)
            button.clicked.connect(partial(self.stackedWidget.setCurrentWidget, window))


class InicioWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(InicioWidget, self).__init__(parent)
        self.setLayout(QtWidgets.QVBoxLayout())
        self.layout().addWidget(QtWidgets.QLabel("Inicio\nVentana"))

class VentasWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(VentasWidget, self).__init__(parent)
        self.setLayout(QtWidgets.QVBoxLayout())
        self.layout().addWidget(QtWidgets.QLabel("Ventas\nVentana"))


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

Note: I have modified the qss by adding rules.

    
answered by 11.07.2018 / 01:14
source