从另一个 class 调用列表实例
calling a list instance from another class
我想访问在我的主 window 上根据用户选择创建的列表,在我的第二个 window.
该列表是在 MainWindow
中使用 on_cc_pick
方法创建的。列表 creditcards
.
在第二个 window,我想在 CreditCardForm
访问它
当我尝试时。MainWindow.creditcards
或 MainWindow.creditcardsb
它告诉我对象没有属性 creditcards/b。
当我尝试在 MainWindow 下做一个全局变量时 Class:
class MainWindow(QtWidgets.QWidget):
creditcards = []
它始终默认为一个空列表。
这是我的代码,本质上我想访问在 class MainWindow
下 def on_cc_pick(self,text):
上创建的名为 creditcardsb
的列表 class CreditCardForm
在 def savecsv(self):
下
class AddCreditCard(QtWidgets.QMainWindow):
def __init__(self, parent = None):
super(AddCreditCard, self).__init__(parent)
creditcardform = CreditCardForm(self)
self.setCentralWidget(creditcardform)
class CreditCardForm(QtWidgets.QWidget):
def savecsv(self):
**print(MainWindow.creditcardsb)**
def __init__(self, parent):
super(CreditCardForm, self).__init__(parent)
self.addname = QtWidgets.QPushButton('Save')
self.connect(self.addname,QtCore.SIGNAL("clicked()"), self.savecsv)
class MainWindow(QtWidgets.QWidget):
def CreateCCForm(self):
self.addwindow.show()
def on_cc_pick(self, text):
NickName = []
Account = []
with open(refdirectory + '/' + str(text) + '.csv') as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
NickName.append(row[0])
Account.append(row[1])
**creditcardsb = list(zip(NickName,Account))
self.creditcards = creditcardsb**
def __init__(self, parent = None):
super(MainWindow,self).__init__(parent)
self.pickcard = QtWidgets.QComboBox(self)
CreditCardNames = ['cc1','cc2','cc3']
for cc in CreditCardNames:
self.pickcard.addItem(cc)
self.addcard = QtWidgets.QPushButton('Add Card')
self.pickcard.activated[str].connect(self.on_cc_pick)
self.connect(self.addcard, QtCore.SIGNAL("clicked()"),self.CreateCCForm)
self.addwindow = AddCreditCard(self)
这些是我正在使用的进口产品,并非都与我的问题相关:
from subprocess import Popen
from selenium import webdriver
from bs4 import BeautifulSoup
import string
import time
import random
import csv
import pandas as pd
from pick import pick
import requests
import re
import urllib.request
from urllib.request import urlopen
from selenium.webdriver.common.by import By
from tkinter import *
import tkinter as tk
import tkinter.filedialog as filedialog
import os
import pyodbc
from decimal import *
import sys
from PySide2 import QtCore, QtGui, QtWidgets
from datetime import datetime, date
from decimal import Decimal
不要使用静态变量,因为要传输的 属性 不依赖于 class,而是依赖于对象。所以正确的做法是创建一个方法来更新 window 的数据。此外,假设 CreditCardForm
将始终是 AddCreditCard
的子代,您可以使用 parentWidget()
从 CreditCardForm
.
获得 AddCreditCard
import os
import csv
from PySide2 import QtCore, QtWidgets
class AddCreditCard(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(AddCreditCard, self).__init__(parent)
creditcardform = CreditCardForm()
self.setCentralWidget(creditcardform)
self._creditcards = []
def setCreditcards(self, creditcards):
self._creditcards = creditcards
def creditcards(self):
return self._creditcards
class CreditCardForm(QtWidgets.QWidget):
def __init__(self, parent=None):
super(CreditCardForm, self).__init__(parent)
self.addname = QtWidgets.QPushButton('Save')
self.addname.clicked.connect(self.savecsv)
lay = QtWidgets.QHBoxLayout(self)
lay.addWidget(self.addname)
@QtCore.Slot()
def savecsv(self):
creditcards = self.parentWidget().creditcards()
print(creditcards)
class MainWindow(QtWidgets.QWidget):
def __init__(self, parent = None):
super(MainWindow,self).__init__(parent)
self.addwindow = AddCreditCard(self)
self.pickcard = QtWidgets.QComboBox()
self.pickcard.activated[str].connect(self.on_cc_pick)
creditcardnames = ['cc1','cc2','cc3']
self.pickcard.addItems(creditcardnames)
self.addcard = QtWidgets.QPushButton('Add Card')
self.addcard.clicked.connect(self.addwindow.show)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.addcard)
lay.addWidget(self.pickcard)
@QtCore.Slot(str)
def on_cc_pick(self, text):
creditcards = []
refdirectory = ""
filename = os.path.join(refdirectory, "{}.csv".format(text))
with open(filename) as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
creditcards.append((row[0], row[1]))
self.addwindow.setCreditcards(creditcards)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
我想访问在我的主 window 上根据用户选择创建的列表,在我的第二个 window.
该列表是在 MainWindow
中使用 on_cc_pick
方法创建的。列表 creditcards
.
在第二个 window,我想在 CreditCardForm
当我尝试时。MainWindow.creditcards
或 MainWindow.creditcardsb
它告诉我对象没有属性 creditcards/b。
当我尝试在 MainWindow 下做一个全局变量时 Class:
class MainWindow(QtWidgets.QWidget):
creditcards = []
它始终默认为一个空列表。
这是我的代码,本质上我想访问在 class MainWindow
下 def on_cc_pick(self,text):
上创建的名为 creditcardsb
的列表 class CreditCardForm
在 def savecsv(self):
class AddCreditCard(QtWidgets.QMainWindow):
def __init__(self, parent = None):
super(AddCreditCard, self).__init__(parent)
creditcardform = CreditCardForm(self)
self.setCentralWidget(creditcardform)
class CreditCardForm(QtWidgets.QWidget):
def savecsv(self):
**print(MainWindow.creditcardsb)**
def __init__(self, parent):
super(CreditCardForm, self).__init__(parent)
self.addname = QtWidgets.QPushButton('Save')
self.connect(self.addname,QtCore.SIGNAL("clicked()"), self.savecsv)
class MainWindow(QtWidgets.QWidget):
def CreateCCForm(self):
self.addwindow.show()
def on_cc_pick(self, text):
NickName = []
Account = []
with open(refdirectory + '/' + str(text) + '.csv') as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
NickName.append(row[0])
Account.append(row[1])
**creditcardsb = list(zip(NickName,Account))
self.creditcards = creditcardsb**
def __init__(self, parent = None):
super(MainWindow,self).__init__(parent)
self.pickcard = QtWidgets.QComboBox(self)
CreditCardNames = ['cc1','cc2','cc3']
for cc in CreditCardNames:
self.pickcard.addItem(cc)
self.addcard = QtWidgets.QPushButton('Add Card')
self.pickcard.activated[str].connect(self.on_cc_pick)
self.connect(self.addcard, QtCore.SIGNAL("clicked()"),self.CreateCCForm)
self.addwindow = AddCreditCard(self)
这些是我正在使用的进口产品,并非都与我的问题相关:
from subprocess import Popen
from selenium import webdriver
from bs4 import BeautifulSoup
import string
import time
import random
import csv
import pandas as pd
from pick import pick
import requests
import re
import urllib.request
from urllib.request import urlopen
from selenium.webdriver.common.by import By
from tkinter import *
import tkinter as tk
import tkinter.filedialog as filedialog
import os
import pyodbc
from decimal import *
import sys
from PySide2 import QtCore, QtGui, QtWidgets
from datetime import datetime, date
from decimal import Decimal
不要使用静态变量,因为要传输的 属性 不依赖于 class,而是依赖于对象。所以正确的做法是创建一个方法来更新 window 的数据。此外,假设 CreditCardForm
将始终是 AddCreditCard
的子代,您可以使用 parentWidget()
从 CreditCardForm
.
AddCreditCard
import os
import csv
from PySide2 import QtCore, QtWidgets
class AddCreditCard(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(AddCreditCard, self).__init__(parent)
creditcardform = CreditCardForm()
self.setCentralWidget(creditcardform)
self._creditcards = []
def setCreditcards(self, creditcards):
self._creditcards = creditcards
def creditcards(self):
return self._creditcards
class CreditCardForm(QtWidgets.QWidget):
def __init__(self, parent=None):
super(CreditCardForm, self).__init__(parent)
self.addname = QtWidgets.QPushButton('Save')
self.addname.clicked.connect(self.savecsv)
lay = QtWidgets.QHBoxLayout(self)
lay.addWidget(self.addname)
@QtCore.Slot()
def savecsv(self):
creditcards = self.parentWidget().creditcards()
print(creditcards)
class MainWindow(QtWidgets.QWidget):
def __init__(self, parent = None):
super(MainWindow,self).__init__(parent)
self.addwindow = AddCreditCard(self)
self.pickcard = QtWidgets.QComboBox()
self.pickcard.activated[str].connect(self.on_cc_pick)
creditcardnames = ['cc1','cc2','cc3']
self.pickcard.addItems(creditcardnames)
self.addcard = QtWidgets.QPushButton('Add Card')
self.addcard.clicked.connect(self.addwindow.show)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.addcard)
lay.addWidget(self.pickcard)
@QtCore.Slot(str)
def on_cc_pick(self, text):
creditcards = []
refdirectory = ""
filename = os.path.join(refdirectory, "{}.csv".format(text))
with open(filename) as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
creditcards.append((row[0], row[1]))
self.addwindow.setCreditcards(creditcards)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())