трудности с печатью ("ОК") всякий раз, когда я нажимаю в любом месте в графическом интерфейсе приложения - PullRequest
0 голосов
/ 21 февраля 2019

Я использовал QT Designer для разработки GUI, затем импортировал файл .ui в свой код Python через:

ui_file = QFile(ui_file)
        ui_file.open(QFile.ReadOnly)
        loader = QUiLoader()
        self.window = loader.load(ui_file)
        ui_file.close()

и все работает нормально, GUI появляется и работает, когда я запускаюPython-код Однако я сталкиваюсь с трудностями в достижении функциональности: «Я хочу печатать (« хорошо ») всякий раз, когда я нажимаю на любую точку в GUI».вот мой код:

import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QComboBox, QCheckBox, QLabel, QPushButton, QFrame, QAction, QDesktopWidget, QTabWidget, QMainWindow, QWidget
from PySide2.QtCore import QFile, QObject,Qt, QPoint
from PySide2 import QtCore, QtGui

class MyApp(QObject):
    def __init__(self, ui_file, parent=None):
        super(MyApp, self).__init__(parent)
        ui_file = QFile(ui_file)
        ui_file.open(QFile.ReadOnly)
        loader = QUiLoader()
        self.window = loader.load(ui_file)
        ui_file.close()
        self.window.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.window.show()

    def mousePressEvent(self, event):    
        print("ok")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = MyApp('GUI.ui')
    sys.exit(app.exec_())

вот файл UI: PS.создайте новый файл GUI.ui и отредактируйте его с помощью блокнота, добавьте следующий код, и вы получите мой файл GUI.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="windowModality">
   <enum>Qt::NonModal</enum>
  </property>
  <property name="enabled">
   <bool>true</bool>
  </property>
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>497</width>
    <height>488</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="contextMenuPolicy">
   <enum>Qt::DefaultContextMenu</enum>
  </property>
  <property name="windowTitle">
   <string>Pirates Online Hub</string>
  </property>
  <property name="windowIcon">
   <iconset>
    <normaloff>Icon.ico</normaloff>Icon.ico</iconset>
  </property>
  <property name="autoFillBackground">
   <bool>false</bool>
  </property>
  <property name="toolButtonStyle">
   <enum>Qt::ToolButtonIconOnly</enum>
  </property>
  <property name="documentMode">
   <bool>false</bool>
  </property>
  <property name="tabShape">
   <enum>QTabWidget::Rounded</enum>
  </property>
  <property name="dockNestingEnabled">
   <bool>false</bool>
  </property>
  <property name="unifiedTitleAndToolBarOnMac">
   <bool>false</bool>
  </property>
  <property name="setSizeGripEnabled" stdset="0">
   <bool>true</bool>
  </property>
  <property name="QMainWindow.setSizeGripEnabled" stdset="0">
   <bool>false</bool>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QTabWidget" name="tabWidget">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>501</width>
      <height>491</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Open Sans Semibold</family>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="autoFillBackground">
     <bool>false</bool>
    </property>
    <property name="styleSheet">
     <string notr="true">QTabBar { qproperty-drawBase: 0; }


QTabWidget::tab-bar
{
    left: px;
    alignment: left;

}

QTabBar::tab
{
    background: transparent;
    color: #757575;
    padding: 15px 5px 15px 5px;
    border: 2px solid #000000;
}

QTabBar::tab:hover
{
    text-decoration: dunderline;

}

QTabBar::tab:selected
{
    color: #DEF600;
    background: qlineargradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #262626, stop: 1.0 #3D3D3D );
    text-decoration: underline;
}

QTabBar::tab:!selected
{
    color: #DEF600;
    background: qlineargradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #262626, stop: 1.0 #3D3D3D );
}

QTabBar::tab {
    /* expand/overlap to the left and right by 4px */
    margin-left:  1px;
    margin-right: 1px;
}</string>
    </property>
    <property name="tabPosition">
     <enum>QTabWidget::North</enum>
    </property>
    <property name="tabShape">
     <enum>QTabWidget::Rounded</enum>
    </property>
    <property name="currentIndex">
     <number>0</number>
    </property>
    <property name="elideMode">
     <enum>Qt::ElideNone</enum>
    </property>
    <property name="documentMode">
     <bool>true</bool>
    </property>
    <property name="tabsClosable">
     <bool>false</bool>
    </property>
    <property name="movable">
     <bool>false</bool>
    </property>
    <property name="tabBarAutoHide">
     <bool>false</bool>
    </property>
    <widget class="QWidget" name="tab">
     <property name="cursor">
      <cursorShape>ArrowCursor</cursorShape>
     </property>
     <attribute name="title">
      <string>Fairy Coins</string>
     </attribute>
     <widget class="QLabel" name="label">
      <property name="geometry">
       <rect>
        <x>200</x>
        <y>5</y>
        <width>87</width>
        <height>26</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <family>Open Sans Semibold</family>
        <pointsize>14</pointsize>
        <weight>75</weight>
        <bold>true</bold>
       </font>
      </property>
      <property name="text">
       <string>Slot Spins</string>
      </property>
      <property name="alignment">
       <set>Qt::AlignCenter</set>
      </property>
     </widget>
     <widget class="QComboBox" name="comboBox">
      <property name="geometry">
       <rect>
        <x>210</x>
        <y>65</y>
        <width>51</width>
        <height>22</height>
       </rect>
      </property>
      <property name="editable">
       <bool>true</bool>
      </property>
     </widget>
     <widget class="QLabel" name="label_2">
      <property name="geometry">
       <rect>
        <x>159</x>
        <y>100</y>
        <width>331</width>
        <height>26</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <family>Open Sans Semibold</family>
        <pointsize>14</pointsize>
        <weight>75</weight>
        <bold>true</bold>
       </font>
      </property>
      <property name="inputMethodHints">
       <set>Qt::ImhNone</set>
      </property>
      <property name="text">
       <string>One Row:       0</string>
      </property>
      <property name="textFormat">
       <enum>Qt::PlainText</enum>
      </property>
     </widget>
     <widget class="QLabel" name="label_3">
      <property name="geometry">
       <rect>
        <x>119</x>
        <y>130</y>
        <width>371</width>
        <height>26</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <family>Open Sans Semibold</family>
        <pointsize>14</pointsize>
        <weight>75</weight>
        <bold>true</bold>
       </font>
      </property>
      <property name="text">
       <string>        Two Rows:     0</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_4">
      <property name="geometry">
       <rect>
        <x>156</x>
        <y>160</y>
        <width>331</width>
        <height>26</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <family>Open Sans Semibold</family>
        <pointsize>14</pointsize>
        <weight>75</weight>
        <bold>true</bold>
       </font>
      </property>
      <property name="text">
       <string>Three Rows:   0</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_9">
      <property name="geometry">
       <rect>
        <x>210</x>
        <y>208</y>
        <width>87</width>
        <height>26</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <family>Open Sans Semibold</family>
        <pointsize>14</pointsize>
        <weight>75</weight>
        <bold>true</bold>
       </font>
      </property>
      <property name="text">
       <string>Stall Price</string>
      </property>
      <property name="alignment">
       <set>Qt::AlignCenter</set>
      </property>
     </widget>
     <widget class="QComboBox" name="comboBox_3">
      <property name="geometry">
       <rect>
        <x>180</x>
        <y>280</y>
        <width>71</width>
        <height>22</height>
       </rect>
      </property>
      <property name="editable">
       <bool>true</bool>
      </property>
     </widget>
     <widget class="QLabel" name="label_10">
      <property name="geometry">
       <rect>
        <x>140</x>
        <y>58</y>
        <width>81</width>
        <height>31</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <family>Open Sans Semibold</family>
        <pointsize>11</pointsize>
        <weight>75</weight>
        <bold>true</bold>
       </font>
      </property>
      <property name="text">
       <string>Amount:</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_11">
      <property name="geometry">
       <rect>
        <x>110</x>
        <y>274</y>
        <width>81</width>
        <height>31</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <family>Open Sans Semibold</family>
        <pointsize>11</pointsize>
        <weight>75</weight>
        <bold>true</bold>
       </font>
      </property>
      <property name="text">
       <string>Amount:</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_12">
      <property name="geometry">
       <rect>
        <x>130</x>
        <y>310</y>
        <width>121</width>
        <height>31</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <family>Open Sans Semibold</family>
        <pointsize>11</pointsize>
        <weight>75</weight>
        <bold>true</bold>
       </font>
      </property>
      <property name="text">
       <string>Price:</string>
      </property>
     </widget>
     <widget class="QComboBox" name="comboBox_4">
      <property name="geometry">
       <rect>
        <x>180</x>
        <y>316</y>
        <width>71</width>
        <height>22</height>
       </rect>
      </property>
      <property name="editable">
       <bool>true</bool>
      </property>
     </widget>
     <widget class="QComboBox" name="comboBox_5">
      <property name="geometry">
       <rect>
        <x>260</x>
        <y>280</y>
        <width>81</width>
        <height>22</height>
       </rect>
      </property>
      <item>
       <property name="text">
        <string>Pieces</string>
       </property>
      </item>
      <item>
       <property name="text">
        <string>Stacks</string>
       </property>
      </item>
     </widget>
     <widget class="QComboBox" name="comboBox_10">
      <property name="geometry">
       <rect>
        <x>260</x>
        <y>316</y>
        <width>81</width>
        <height>22</height>
       </rect>
      </property>
      <item>
       <property name="text">
        <string>Per Piece</string>
       </property>
      </item>
      <item>
       <property name="text">
        <string>Per Stack</string>
       </property>
      </item>
     </widget>
     <widget class="QLabel" name="label_21">
      <property name="geometry">
       <rect>
        <x>200</x>
        <y>390</y>
        <width>281</width>
        <height>51</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <pointsize>14</pointsize>
       </font>
      </property>
      <property name="text">
       <string>Gold: 0</string>
      </property>
     </widget>
     <widget class="QComboBox" name="comboBox_16">
      <property name="geometry">
       <rect>
        <x>250</x>
        <y>360</y>
        <width>69</width>
        <height>22</height>
       </rect>
      </property>
      <property name="currentIndex">
       <number>0</number>
      </property>
      <item>
       <property name="text">
        <string> Gold (g)</string>
       </property>
      </item>
      <item>
       <property name="text">
        <string> Kilo (k)</string>
       </property>
      </item>
      <item>
       <property name="text">
        <string> Million (m)</string>
       </property>
      </item>
      <item>
       <property name="text">
        <string> Billion (b)</string>
       </property>
      </item>
     </widget>
     <widget class="QLabel" name="label_31">
      <property name="geometry">
       <rect>
        <x>170</x>
        <y>363</y>
        <width>121</width>
        <height>16</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <family>Open Sans Semibold</family>
        <pointsize>11</pointsize>
        <weight>75</weight>
        <bold>true</bold>
       </font>
      </property>
      <property name="text">
       <string>Price Unit:</string>
      </property>
     </widget>
     <widget class="QComboBox" name="comboBox_23">
      <property name="geometry">
       <rect>
        <x>270</x>
        <y>64</y>
        <width>61</width>
        <height>22</height>
       </rect>
      </property>
      <property name="editable">
       <bool>false</bool>
      </property>
      <item>
       <property name="text">
        <string>Pieces</string>
       </property>
      </item>
      <item>
       <property name="text">
        <string>Stacks</string>
       </property>
      </item>
     </widget>
     <widget class="Line" name="line">
      <property name="geometry">
       <rect>
        <x>-20</x>
        <y>190</y>
        <width>561</width>
        <height>16</height>
       </rect>
      </property>
      <property name="frameShadow">
       <enum>QFrame::Plain</enum>
      </property>
      <property name="lineWidth">
       <number>2</number>
      </property>
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
     </widget>
     <widget class="Line" name="line_3">
      <property name="geometry">
       <rect>
        <x>-40</x>
        <y>430</y>
        <width>561</width>
        <height>16</height>
       </rect>
      </property>
      <property name="frameShadow">
       <enum>QFrame::Plain</enum>
      </property>
      <property name="lineWidth">
       <number>2</number>
      </property>
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
     </widget>
    </widget>
    <widget class="QWidget" name="tab_2">
     <attribute name="title">
      <string>Skills</string>
     </attribute>
    </widget>
   </widget>
   <widget class="QLabel" name="label_5">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>971</width>
      <height>881</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color:qradialgradient(spread:reflect, cx:0.5, cy:0.5, radius:0.5, fx:0.511364, fy:0.506, stop:0 rgba(24, 132, 214, 255), stop:1 rgba(109, 187, 238, 255))</string>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
   <widget class="Line" name="line_2">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>-7</y>
      <width>561</width>
      <height>16</height>
     </rect>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Plain</enum>
    </property>
    <property name="lineWidth">
     <number>2</number>
    </property>
    <property name="orientation">
     <enum>Qt::Horizontal</enum>
    </property>
   </widget>
   <widget class="Line" name="line_4">
    <property name="geometry">
     <rect>
      <x>-9</x>
      <y>0</y>
      <width>20</width>
      <height>1001</height>
     </rect>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Plain</enum>
    </property>
    <property name="lineWidth">
     <number>2</number>
    </property>
    <property name="orientation">
     <enum>Qt::Vertical</enum>
    </property>
   </widget>
   <widget class="Line" name="line_5">
    <property name="geometry">
     <rect>
      <x>486</x>
      <y>0</y>
      <width>20</width>
      <height>1001</height>
     </rect>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Plain</enum>
    </property>
    <property name="lineWidth">
     <number>2</number>
    </property>
    <property name="orientation">
     <enum>Qt::Vertical</enum>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>467</x>
      <y>1</y>
      <width>30</width>
      <height>23</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Terminal</family>
      <pointsize>14</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="styleSheet">
     <string notr="true">background: transparent; color:rgb(49, 49, 49)</string>
    </property>
    <property name="text">
     <string>X</string>
    </property>
   </widget>
   <zorder>label_5</zorder>
   <zorder>tabWidget</zorder>
   <zorder>line_2</zorder>
   <zorder>line_4</zorder>
   <zorder>line_5</zorder>
   <zorder>pushButton_2</zorder>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

1 Ответ

0 голосов
/ 21 февраля 2019

MyApp не является виджетом, поэтому mousePressEvent не существует, и меньше окна.Поэтому в этих случаях вы должны использовать фильтр событий:

import sys
from PySide2 import QtCore, QtWidgets, QtUiTools

class MyApp(QtCore.QObject):
    def __init__(self, ui_file, parent=None):
        super(MyApp, self).__init__(parent)
        ui_file = QtCore.QFile(ui_file)
        if ui_file.open(QtCore.QFile.ReadOnly):
            loader = QtUiTools.QUiLoader()
            self.window = loader.load(ui_file)
            self.window.installEventFilter(self)
            self.window.setWindowFlags(QtCore.Qt.FramelessWindowHint)
            self.window.show()

    def eventFilter(self, obj, event):
        if obj is self.window:
            if event.type() == QtCore.QEvent.MouseButtonPress:
                print("ok")
        return super(MyApp, self).eventFilter(obj, event)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    form = MyApp('GUI.ui')
    sys.exit(app.exec_())
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...