我有一个复杂的程序,需要连接多个窗口.不幸的是,我似乎没有完全理解这样做所需的概念/步骤,因此,如果有人能很好地解释这些步骤/过程,我会得到额外的分数.在我当前的程序中,我有一个项目列表.一旦我通过将它们移到右边的列表小部件来 Select 它们,我需要它们转到第三个窗口.第三个窗口应通过单击第二个窗口上的点来激活.程序运行并适当显示第二个窗口,但dots按钮的信号/插槽连接不起作用.然而,其余的代码仍在运行,因为如果我切换工具箱以显示第三个窗口,则该部分的执行情况与预期一致.下面是我的代码,同样,没有返回任何错误,但单击第二个窗口上的点按钮没有任何作用.

还有一个问题,我是在第二个类中实例化第三个窗口,还是只在主窗口中实例化?再一次,我正在努力完全理解这个过程,我还需要多次这样做.

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QListWidget, QLineEdit, QTextEdit, QGridLayout, QHBoxLayout, QVBoxLayout, QSizePolicy, QFileDialog, QTabWidget, QCheckBox
import PyQt5.QtGui as qtg
import glob
import os
from PyQt5.QtCore import Qt, QSettings
import inspect
from PyQt5 import QtCore
import pandas as pd 
import pathlib
import pyreadstat
import json


class ThirdWindow(QWidget):
    def __init__(self):
        super().__init__()
        
        
        self.layout = QGridLayout()
        self.setLayout(self.layout)
        self.allVariables = QListWidget()
        self.variablesSelected = QListWidget()
        #self.allVariables.insertItem(0, 'Hello')
        self.layout.addWidget(self.allVariables, 1,0)
        self.layout.addWidget(self.variablesSelected, 1, 1)
        
    def setItems(self, items):
        self.allVariables.clear()
        for item in items:
            self.allVariables.addItem(item)
     
    
     
class SecondWindow(QWidget):
    def __init__(self):
        super().__init__()
        
        ##not sure if I am supposed to instantiate this here or only in the main window class
        self.thirdWindow = ThirdWindow()
        
    
        self.layout = QGridLayout(self)
        self.by = QLabel("By")
        self.byVariables = QLineEdit()
        self.byButton = QPushButton("...")
        self.layout.addWidget(self.by, 1, 0)
        self.layout.addWidget(self.byVariables, 2, 0)
        self.layout.addWidget(self.byButton, 2, 1)
        
       
    def seconddWindowConnections(self):
        self.byButton.clicked.connect(self.show_third_window)
        #self.buttons['Toolkit'].clicked.connect(self.show_new_window)   
        
    def show_third_window(self):
        self.thirdWindow.show()
        
class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        # Add a title
        self.setWindowTitle("GUI Querying Program")

        self.layout = QHBoxLayout()
        self.setLayout(self.layout)
        self.initUI()
        self.setButtonConnections()
        
        self.sw = SecondWindow()
        self.tw = ThirdWindow()
        

    def initUI(self):
        subLayouts = {}

        subLayouts['LeftColumn'] = QGridLayout()
    
        self.layout.addLayout(subLayouts['LeftColumn'],1)
        
        # Buttons
        self.buttons = {}
        self.buttons['addVariable'] = QPushButton('>')
        self.buttons['removeVariable'] = QPushButton('<')
        self.buttons['Toolkit'] = QPushButton('Toolkit')
        
        
        self.variables = QListWidget()
        self.selectedVariables = QListWidget()
        
        subLayouts['LeftColumn'].addWidget(self.variables, 7,0,4,1)
        subLayouts['LeftColumn'].addWidget(self.selectedVariables, 7,1,4,1)
        subLayouts['LeftColumn'].addWidget(self.buttons['addVariable'], 10,0,1,1)
        subLayouts['LeftColumn'].addWidget(self.buttons['removeVariable'], 10,1,1,1)
        subLayouts['LeftColumn'].addWidget(self.buttons['Toolkit'], 11,1,1,1)
        
        names = ['apple', 'banana', 'Cherry']
        self.variables.insertItems(0, names)
        
    def setButtonConnections(self):
        self.buttons['addVariable'].clicked.connect(self.add_variable)
        self.buttons['Toolkit'].clicked.connect(self.show_new_window)   
        self.buttons['Toolkit'].clicked.connect(self.add_selected_variables)
        
    def add_variable(self):
        for item in self.variables.selectedItems():
            self.selectedVariables.addItem(item.clone())

    def show_new_window(self):
        self.sw.show()
        
    def add_selected_variables(self):
        items = []
        for i in range(self.selectedVariables.count()):
            items.append(self.selectedVariables.item(i).clone())
        self.tw.setItems(items)


if __name__ == "__main__":
    import sys
    app = QApplication([])
    mw = MainWindow()
    mw.show()
    app.exec()

推荐答案

代码的主要问题是从来没有调用过secondWindowConnections,所以按钮实际上什么都不做.我纠正了这一点,并修复了我在下面的示例中发现的其他一些问题.我省略了我没有做任何更改的部分,我做了所有更改,我做了内联注释来解释它们:

class SecondWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.thirdWindow = None  # dont initialize until neccessary
        self.thirdWindowItems = []
        self.layout = QGridLayout(self)
        self.by = QLabel("By")
        self.byVariables = QLineEdit()
        self.byButton = QPushButton("...")
        self.layout.addWidget(self.by, 1, 0)
        self.layout.addWidget(self.byVariables, 2, 0)
        self.layout.addWidget(self.byButton, 2, 1)
        self.secondWindowConnections()  #  Run this to setup the
                                       #  signal for the third window.
    def secondWindowConnections(self):   # this had a typo
        self.byButton.clicked.connect(self.show_third_window)

    def show_third_window(self):
        if self.thirdWindow is None:           # if window has been created yet
            self.thirdWindow = ThirdWindow()   # create window
        if not self.thirdWindow.isVisible():   # if window is showing
            self.thirdWindow.show()            # show window
        self.thirdWindow.setItems(self.thirdWindowItems)  # send items to window

    def send_items(self, items):       # this is to collect the variable that
        self.thirdWindowItems = items  # move to the third window

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        # Add a title
        self.setWindowTitle("GUI Querying Program")
        self.layout = QHBoxLayout()
        self.setLayout(self.layout)
        self.initUI()
        self.setButtonConnections()
        self.sw = None    # dont initialize until neccessary.

    def initUI(self):
        subLayouts = {}
        subLayouts['LeftColumn'] = QGridLayout()
        self.layout.addLayout(subLayouts['LeftColumn'],1)
        self.buttons = {}
        self.buttons['addVariable'] = QPushButton('>')
        self.buttons['removeVariable'] = QPushButton('<')
        self.buttons['Toolkit'] = QPushButton('Toolkit')
        self.variables = QListWidget()
        self.selectedVariables = QListWidget()
        subLayouts['LeftColumn'].addWidget(self.variables, 7,0,4,1)
        subLayouts['LeftColumn'].addWidget(self.selectedVariables, 7,1,4,1)
        subLayouts['LeftColumn'].addWidget(self.buttons['addVariable'], 10,0,1,1)
        subLayouts['LeftColumn'].addWidget(self.buttons['removeVariable'], 10,1,1,1)
        subLayouts['LeftColumn'].addWidget(self.buttons['Toolkit'], 11,1,1,1)
        names = ['apple', 'banana', 'Cherry']
        self.variables.insertItems(0, names)

    def setButtonConnections(self):
        self.buttons['addVariable'].clicked.connect(self.add_variable)
        self.buttons['Toolkit'].clicked.connect(self.show_new_window)
        # self.buttons['Toolkit'].clicked.connect(self.add_selected_variables)
        # only use one connnect slot

    def add_variable(self):
        for item in self.variables.selectedItems():
            self.selectedVariables.addItem(item.clone())

    def show_new_window(self):     
        if self.sw is None:   #  check if window has been constructed
            self.sw = SecondWindow()  # construct window
        if not self.sw.isVisible():    #  If winow is not showing
            self.sw.show()         #  show window
        self.sw.send_items(self.add_selected_variables())   # send selected 
                                                            # variables to second window

    def add_selected_variables(self):
        items = []
        for i in range(self.selectedVariables.count()):
            items.append(self.selectedVariables.item(i).clone())
        # self.tw.setItems(items) ...  self.tw doesnt exist so return them
        return items 

Python相关问答推荐

如何在Power Query中按名称和时间总和进行分组

Python中的Pool.starmap异常处理

基本链合同的地址是如何计算的?

使用多个性能指标执行循环特征消除

Polars:使用列值引用when / then表达中的其他列

如何使用entry.bind(FocusIn,self.Method_calling)用于使用网格/列表创建的收件箱

如何处理嵌套的SON?

根据条件将新值添加到下面的行或下面新创建的行中

无法通过python-jira访问jira工作日志(log)中的 comments

如何从.cgi网站刮一张表到rame?

如何将一个动态分配的C数组转换为Numpy数组,并在C扩展模块中返回给Python

Godot:需要碰撞的对象的AdditionerBody2D或Area2D以及queue_free?

Pandas计数符合某些条件的特定列的数量

有没有一种ONE—LINER的方法给一个框架的每一行一个由整数和字符串组成的唯一id?

在单个对象中解析多个Python数据帧

计算天数

Pandas Data Wrangling/Dataframe Assignment

基于形状而非距离的两个numpy数组相似性

交替字符串位置的正则表达式

Python日志(log)模块如何在将消息发送到父日志(log)记录器之前向消息添加类实例变量