0
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)

this is the first window

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")

the "enter data" button is this button I need connected to the second window

    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)

this is the second window

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

please help because it will be much appreciated as I am extremely confused xxx

1 Answers1

0

You have to add the connections between the widgets upon creation or later on by passing their references. In both case you them to be visible in your current scope so that you can manage their properties. In your case since you are creating both top-level widgets inside functions you can add a return which returns the object of the created widget. After that you can grab both returned widgets and connect them accordingly.

Here is an example for doing that (you can adjust it to your own):

#!/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()
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161