如何将“输入数据”按钮连接到第二个窗口

时间:2016-03-01 19:41:49

标签: python pyqt4

import sys 

from PyQt4.QtCore import * 
from PyQt4.QtGui import *


class StockWindow(QMainWindow): #creates the main Window in the stock simulation
#constructor allows us create an object
def __init__(self): 

    super().__init__() #calls super class constructor

    self.setWindowTitle("Stock Simulator") #sets the title of the window to "stock simulator"
    #self.create_select_stock_layout()

    self.create_select_stock_layout()


    #this is a stack layout which allows us to maintain multiple widget for that central widget so I can switch between the different widgets
    self.stacked_layout = QStackedLayout()#holds the various layouts this window needs
    self.stacked_layout.addWidget(self.select_stock_widget)

    #sets the central widget to display the layout
    self.central_widget = QWidget()
    self.central_widget.setLayout(self.stacked_layout)
    self.setCentralWidget(self.central_widget)

这是第一个窗口

def create_select_stock_layout(self): #creates first window in the stock simulation
    self.stock_radio_buttons = RadioButtonWidget("Stock Simulation", "Please select how you want to receive the data")

“输入数据”按钮是我需要连接到第二个窗口的按钮

    self.enter_data_button = QPushButton("Enter Data")
    self.import_data_button = QPushButton("Import Data")


    self.initial_layout = QVBoxLayout()
    self.initial_layout.addWidget(self.stock_radio_buttons)
    self.initial_layout.addWidget(self.enter_data_button)
    self.initial_layout.addWidget(self.import_data_button)



     #adding layout to widget
    self.select_stock_widget = QWidget() #displays this widget
    self.select_stock_widget.setLayout(self.initial_layout)

    #self.enter_data_button.clicked.connect(create_view_stock_layout)

这是第二个窗口

def create_view_stock_layout(self, stock_type): #creates the second window

    self.price_label = QLabel("price") #adds label to the method 
    self.time_label = QLabel("time") #adds label to the method

    #adds the text boxes to method
    self.price1_line_edit = QLineEdit()
    self.price2_line_edit = QLineEdit()
    self.price3_line_edit = QLineEdit()

    #adds the text boxes to method
    self.time1_line_edit = QLineEdit()
    self.time2_line_edit = QLineEdit()
    self.time3_line_edit = QLineEdit()

    self.create_graph_button = QPushButton("Create Graph") #adds the button the user clicks to create the graph


    #adds grid layouts
    self.stock_grid = QGridLayout() #this represents the layout of the whole window
    self.status_grid = QGridLayout() #this represents the layout for the textboxes


    #adds label widgets to the status layout
    self.status_grid.addWidget(self.price_label,0,0)
    #creates three textboxes in a column for the price
    self.status_grid.addWidget(self.price1_line_edit,1,0)
    self.status_grid.addWidget(self.price2_line_edit,2,0)
    self.status_grid.addWidget(self.price3_line_edit,3,0)

    #adds label widgets to the status layout
    self.status_grid.addWidget(self.time_label,0,1)
    #creates three textboxes in a column for the time
    self.status_grid.addWidget(self.time1_line_edit,1,1)
    self.status_grid.addWidget(self.time2_line_edit,2,1)
    self.status_grid.addWidget(self.time3_line_edit,3,1)



    self.stock_grid.addLayout(self.status_grid,0,0)#adds the status grid of the text boxes into the top of the stock grid
    self.stock_grid.addWidget(self.create_graph_button,1,0) #adds the push button to the bottom of the stock grid


    #creates the widget to display the grow layout
    self.view_stock_widget = QWidget()

    self.view_stock_widget.setLayout(self.stock_grid)







class RadioButtonWidget(QWidget): #creates a reusable component so radiobuttons can be created from a given list
def __init__(self, label, instruction):
    super().__init__()
    self.title_label = QLabel(label) #creates the label "stock simulation"
    self.radio_group_box = QGroupBox(instruction) #creates the instuction "Please click a button"

    self.main_layout = QVBoxLayout()
    self.main_layout.addWidget(self.title_label)

    self.setLayout(self.main_layout)

def selected_button(self):
    return self.radio_button_group.checkedId()


def main():

stock_simulation = QApplication(sys.argv) #makes sure the event loop is there for handling events such as button clicking
                                          #sys.argv allows us to pass command line parameters through PyQT                                     
stock_window = StockWindow()#creates new instance of main window

stock_window.show() #makes the instance variable

stock_window.raise_() #raises instance to top of the stack 

stock_simulation.exec_()# it monitors the application for events




if __name__ == "__main__": #This is the main body of the program and where the classes are called 

main()#the function that wraps all the other functions within one module

请帮助,因为我非常感激,因为我非常困惑xxx

1 个答案:

答案 0 :(得分:0)

您必须在创建时或稍后通过传递其引用添加小部件之间的连接。在这两种情况下,您都可以在当前范围内看到它们,以便您可以管理它们的属性。在您的情况下,因为您在函数内创建了两个顶级窗口小部件,您可以添加return,它返回已创建窗口小部件的对象。之后,您可以抓取两个返回的小部件并相应地连接它们。

以下是这样做的示例(您可以根据自己的需要进行调整):

#!/usr/bin/python
import sys
from PyQt4 import Qt
from PyQt4.QtGui import QWidget, QCheckBox, QLabel, QApplication, QLineEdit, QVBoxLayout

class Example1(QWidget):
    def __init__(self):
        super(Example1, self).__init__()
        self.initUI()

    def initUI(self):      
        layout = QVBoxLayout(self)
        self.text_edit = QLineEdit(self)
        self.text_edit.setFixedWidth(100)
        layout.addWidget(self.text_edit)

        self.setWindowTitle('Edit text')
        self.setLayout(layout)
        self.resize(layout.sizeHint())
        self.show()

class Example2(QWidget):
    def __init__(self):
        super(Example2, self).__init__()
        self.initUI()

    def initUI(self):      
        layout = QVBoxLayout(self)
        self.text_show = QLabel(self)
        self.text_show.setFixedWidth(100)
        layout.addWidget(self.text_show)

        self.setWindowTitle('Show text')
        self.setLayout(layout)
        self.resize(layout.sizeHint())


# Function used for the creation of the widgets
def createWidget(wtype):
  if wtype == 'example1': return Example1()
  elif wtype == 'example2' : return Example2()
  else: return None

# Common scope for both widgets where the connection of signals and slots can be established
def createExamples():
  ex1 = createWidget('example1')
  ex2 = createWidget('example2')

  # Connect the QLineEdit to the QLabel
  ex1.text_edit.textChanged.connect(ex2.text_show.setText)

  return (ex1, ex2)

def main():

    app = QApplication(sys.argv)
    # Display the widgets
    ex1, ex2 = createExamples()
    ex1.show()
    ex2.show()
    sys.exit(app.exec_())
if __name__ == '__main__':
    main()