使用多个文件构建一个GUI,如何在子GUI文件之间传输信号?

时间:2019-11-14 13:43:31

标签: python pyqt pyqt5

我的项目如下:

project
    |___ proj.py
    |___ gui
            |___ guiMain.py 
            |___ guiPart01.py 
            |___ guiPart02.py 
            |___ guiPart03.py 
            |___ guiPart04.py 
    |___ otherModels

因为我有一个复杂的GUI,所以我想将GUIMain分成几个子部分。但是子GUI文件之间必须有信号和插槽。我的问题是,我不知道如何在子GUI之间传输信号。

下面我发布代码。

确切的问题是:我想直接从guiPart2.py更改guiPart1.py中的lineEdit ,如果我应该更改{{ 1}}。

guiMain.py

guiMain.py

guiPart1.py

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from gui.guiPart1 import GUIPart1
from gui.guiPart2 import GUIPart2

class GUIMain(QMainWindow):
    def __init__(self):
        super(GUIMain, self).__init__()
        self.init()

    def init(self):
        self.lytMain = QVBoxLayout(self)
        self.lytMain.addLayout(GUIPart1.createLayout_Part1(self))
        self.lytMain.addLayout(GUIPart2.createLayout_Part2(self))

        self.wgtMain = QWidget(self)
        self.wgtMain.setLayout(self.lytMain)

        self.setCentralWidget(self.wgtMain)

guiPart2.py

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class GUIPart1(QWidget):
    def __init__(self):
        super(GUIPart1, self).__init__()
        self.createLayout_Part1()

    def createLayout_Part1(self):
        self.lineEdit_Part1 = QLineEdit("Part1", self)

        self.lytPart1 = QVBoxLayout(self)
        self.lytPart1.addWidget(self.lineEdit_Part1)
        return self.lytPart1 

任何帮助都将受到赞赏。谢谢。

1 个答案:

答案 0 :(得分:0)

我注意到需要注意的地方。试试吧:

## load data
library(maps)
mapStates = map("state", fill = TRUE, plot = FALSE)

## shiny app
library(shiny)
library(leaflet)

ui <- fluidPage(
  leafletOutput("map")
)

server <- function(input, output) {

  output$map <- renderLeaflet({
    leaflet(data = mapStates) %>% addTiles() %>%
      addPolygons(fillColor = topo.colors(10, alpha = NULL))
  })

  ## Block 1
  observe({
    input$map_click
    print("Block 1 executed")
  })

  ## Block 2
  observe({
    input$map_shape_click
    print("Block 2 executed")
  })

}
shinyApp(ui = ui, server = server)

enter image description here