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)
這是第一個窗口如何連接按鈕「輸入數據」到第二個窗口
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