![](/img/trans.png)
[英]How do I create an exe file of gui made using pyqt5?(There are several ui files)
[英]How to create and access several datasets in pyQt5 GUI on python?
我的 PyQt5 GUI 代码有问题。 我想要做的是使用 actionLoad_file 菜单向用户询问文件。 然后我想使用位于另一个文件中的 LoadFile 函数导入数据集。 问题是我不知道如何将用户选择的文件的名称分配给我的 LoadFile 函数生成的数据帧。 目标是允许多次导入并为每次点击生成一个数据框。 此外,我不知道如何以另一种方法(例如绘图)访问我的数据框。
我希望我很清楚......在此先感谢您的帮助。 这是主要功能
import sys
import tkinter
import os.path
from tkinter.filedialog import askopenfilename
import matplotlib
from load_function import LoadFile #Importing homemade functions
from PyQt5 import QtCore, QtGui, QtWidgets, uic
import matplotlib.pyplot as plt
class gui_main(QtWidgets.QMainWindow):
def __init__(self):
super(gui_main, self).__init__()
uic.loadUi('GUI.ui', self)$
self.plot_button.clicked.connect(self.plotting)
self.actionLoad_file.triggered.connect(self.load_file_method)
self.show()
def load_file_method(self):
tkinter.Tk().withdraw()
filepath = askopenfilename()
filename = os.path.basename(filepath)
file,extension = filename.rsplit('.')
vars()[file] = LoadFile(filepath)
gui_main.list_loaded_file.addItem(filename)
def plotting(self):
plt.scatter(file.data["x"],file.data["y"])
plt.show()
这是 LoadFile 函数:
import pandas
from sklearn import preprocessing
class LoadFile:
def __init__(self,file):
self.data = pandas.read_csv(open(file), delimiter=",")
当Qt
内置了一个完美可用的文件对话框时,我不确定你为什么要使用tkinter
文件对话框。
无论如何,正如我所理解的问题,您希望能够读取多个文件,从这些文件中的数据创建数据框,并将文件名存储在我假设是名为list_loaded_file
的QListWidget
。 然后,您希望能够在此列表中选择一个文件并绘制相应的数据。 如果这确实是您想要的,那么您可以在通过item.setData(role, data)
将数据框作为数据添加到列表小部件时将它们添加到列表小部件中,其中role
是某个整数> = QtCore.Qt.UserRole
。 然后可以通过item.data(role)
检索此数据框。
以下是您的示例的外观。 在本例中,按下绘图按钮将绘制列表中第一个选定项目的数据。
import os.path
import pandas as pd
from PyQt5 import QtCore, QtWidgets, uic
import matplotlib.pyplot as plt
from load_function import LoadFile
class gui_main(QtWidgets.QMainWindow):
# role for storing the data frames in the items
USERDATA_ROLE = QtCore.Qt.UserRole
def __init__(self):
# the same as before
def load_file_method(self):
# I'm using a QFileDialog to get the file path
filepath, _ = QtWidgets.QFileDialog.getOpenFileName()
if not filepath:
return
filename = os.path.basename(filepath)
# read data and create data frame
dataframe = LoadFile(filepath)
# create list widget item and store data in item
item = QtWidgets.QListWidgetItem(filename)
item.setData(self.USERDATA_ROLE, dataframe)
self.list_view.addItem(item)
def plotting(self):
# select first selected item in file list.
items = self.list_view.selectedItems()
if not items:
return
item = items[0]
# retrieve and plot data
data = item.data(self.USERDATA_ROLE)
plt.scatter(data["x"], data["y"])
plt.show()
if __name__ == "__main__":
app = QtWidgets.QApplication([])
main = gui_main()
main.show()
app.exec()
您可能正在寻找这样的东西(在我的情况下只是加载一个 .ini 文件)来放入您的MainWindow
,并使用实际的 PyQt5 将filename
与self
一起存储在那里:
BASEDIR = os.path.dirname(__file__)
def readFile(self): # ? Open
"""Restores GUI user input from a config file"""
#* File dialog
self.filename, _ = qtw.QFileDialog.getOpenFileName(
self,
"Select a configuration file to load…",
BASEDIR,
'Configuration Files (*.ini)',
options=qtw.QFileDialog.DontResolveSymlinks
)
#* Invalid file or none
if self.filename == "":
qtw.QMessageBox.critical(
self, "Operation aborted", "Empty filename or none selected. \n Please try again.",
qtw.QMessageBox.Ok
)
self.statusBar().showMessage("Select a valid configuration file")
#* Valid file
else:
if self.filename.lower().endswith('.ini'):
try:
<...> # your code
except Exception or TypeError as e:
qtw.QMessageBox.critical(self, 'Error', f"Could not open settings: {e}")
else:
qtw.QMessageBox.critical(
self, "Invalid file type", "Please select a .ini file.", qtw.QMessageBox.Ok
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.