如何连接按钮“输入数据"?到第二个窗口 [英] How do I connect the push button "Enter data" to the second window

查看:74
本文介绍了如何连接按钮“输入数据"?到第二个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

输入数据"按钮是这个按钮我需要连接到第二个窗口

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)

这是第二个窗口

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

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

推荐答案

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

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

这篇关于如何连接按钮“输入数据"?到第二个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆