PyQt QFileDialog getOpenFileName 无法从命令行运行 (windows)
PyQt QFileDialog getOpenFileName not working from command line (windows)
我正在尝试制作一个 gui (Qt Designer) 来导入 excel 文件并在 gui 中显示数据。
当我从我的 IDE (Spyder) 中 运行 它时,该脚本工作正常,但是如果我从命令 window 或通过打开来自 windows 资源管理器的 python 文件,导入功能不起作用。 (gui 启动正常,但是当按下导入按钮并 selected 文件时,没有任何反应,也没有产生错误。当从 Spyder 运行ning 时,数据被导入并显示在如预期的 gui)。
如果我预先 select 文件位置(在下面的代码中注释掉),那么脚本可以从命令行或通过从资源管理器中单击来正常工作。
感谢您的帮助!
Python2.7(蟒蛇),Windows10,PyQt4
import sys
from PyQt4 import QtGui
from excel_import_gui import Ui_MainWindow
import xlrd
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setupSignals()
def set_import_data(self,data_to_import,table_to_change):
for row in range(len(data_to_import)):
for col in range(len(data_to_import[0])):
table_to_change.setRowCount(len(data_to_import))
table_to_change.setColumnCount(len(data_to_import[0]))
item = data_to_import[row][col]
table_to_change.setItem(row,col,QtGui.QTableWidgetItem(str(item)))
def setupSignals(self):
self.ui.importData_btn.clicked.connect(self.select_file)
def select_file(self):
excel_file = QtGui.QFileDialog.getOpenFileName(self,
"Select Excel file to import","","Excel (*.xls *.xlsx)")
# excel_file = "C:/Users/Ben/Work/Python tests/Qt GUIs/Excel_import_GUI/fish_test.xlsx"
if excel_file:
open_excel_file = xlrd.open_workbook(excel_file)
self.start_import_data(open_excel_file)
def start_import_data(self, workbook):
#import data from excel file
workbook_data = []
for sheetNum in range (workbook.nsheets):
worksheet = workbook.sheet_by_index(sheetNum)
workbook_data.append([[worksheet.cell_value(row,col) for col in range (worksheet.ncols)] for row in range(worksheet.nrows)])
# Set each worksheet of workbook_data to each tab in GUI widget
self.set_import_data(workbook_data[0],self.ui.fish_table)
self.set_import_data(workbook_data[1],self.ui.boats_table)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
好吧,我自己找到了一个解决方案,将 excel_file 变量转换为字符串。
excel_file = str(QtGui.QFileDialog.getOpenFileName(self, "Select Excel file to import","","Excel (*.xls *.xlsx)"))
我正在尝试制作一个 gui (Qt Designer) 来导入 excel 文件并在 gui 中显示数据。
当我从我的 IDE (Spyder) 中 运行 它时,该脚本工作正常,但是如果我从命令 window 或通过打开来自 windows 资源管理器的 python 文件,导入功能不起作用。 (gui 启动正常,但是当按下导入按钮并 selected 文件时,没有任何反应,也没有产生错误。当从 Spyder 运行ning 时,数据被导入并显示在如预期的 gui)。
如果我预先 select 文件位置(在下面的代码中注释掉),那么脚本可以从命令行或通过从资源管理器中单击来正常工作。
感谢您的帮助!
Python2.7(蟒蛇),Windows10,PyQt4
import sys
from PyQt4 import QtGui
from excel_import_gui import Ui_MainWindow
import xlrd
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setupSignals()
def set_import_data(self,data_to_import,table_to_change):
for row in range(len(data_to_import)):
for col in range(len(data_to_import[0])):
table_to_change.setRowCount(len(data_to_import))
table_to_change.setColumnCount(len(data_to_import[0]))
item = data_to_import[row][col]
table_to_change.setItem(row,col,QtGui.QTableWidgetItem(str(item)))
def setupSignals(self):
self.ui.importData_btn.clicked.connect(self.select_file)
def select_file(self):
excel_file = QtGui.QFileDialog.getOpenFileName(self,
"Select Excel file to import","","Excel (*.xls *.xlsx)")
# excel_file = "C:/Users/Ben/Work/Python tests/Qt GUIs/Excel_import_GUI/fish_test.xlsx"
if excel_file:
open_excel_file = xlrd.open_workbook(excel_file)
self.start_import_data(open_excel_file)
def start_import_data(self, workbook):
#import data from excel file
workbook_data = []
for sheetNum in range (workbook.nsheets):
worksheet = workbook.sheet_by_index(sheetNum)
workbook_data.append([[worksheet.cell_value(row,col) for col in range (worksheet.ncols)] for row in range(worksheet.nrows)])
# Set each worksheet of workbook_data to each tab in GUI widget
self.set_import_data(workbook_data[0],self.ui.fish_table)
self.set_import_data(workbook_data[1],self.ui.boats_table)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
好吧,我自己找到了一个解决方案,将 excel_file 变量转换为字符串。
excel_file = str(QtGui.QFileDialog.getOpenFileName(self, "Select Excel file to import","","Excel (*.xls *.xlsx)"))