PyQt4 - 如何返回和保存所选目录的路径



我正在尝试创建一个弹出向导,该向导将让用户选择一个目录路径,将该路径保存到变量中,然后返回该路径以在另一个脚本中使用。

我对Python编码相对较新,我只是在玩。我尝试了 Tkinter,但不喜欢图书馆的外观/感觉。虽然 PyQt 总体上更好用。我尝试以几种不同的方式设置按钮,但收效甚微。我尝试使用YouTube视频中显示的"self"和"self.variable",但我不熟悉它是如何帮助和使用。

当选择了路径时,它将打印输出,但它似乎没有返回要使用的路径,如果是,我不知道它是如何这样做以及如何在其他脚本中使用它。

作为旁注:我在QtGui.QBoxLayout((中放了一个"2",因为没有它,代码将无法工作。

import sys
import os
from PyQt4 import QtGui

class Window(QtGui.QWizard):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(550, 350, 700, 600)
self.setWindowTitle("Output Directory")
path = self.home_menu()
print("This path: {}".format(path))
return path
def home_menu(self):   
out_dir = QtGui.QPushButton("Output Directory", self)
btn1 = QtGui.QPushButton("Confirm", self)
path = self.file_open
out_path = out_dir.clicked.connect(path)
print('Out_path: {}nVariable Type: {}'.format(out_path, type(out_path)))
btn1.clicked.connect(self.using_path) #does nothing
btn1.resize(112, 35)
btn1.move(556, 535)
out_dir.resize(625,175)
out_dir.move(30,130)

layout = QtGui.QBoxLayout(2) #Don't know why I put '2' but code does not work without it.
layout.addWidget(btn1)
layout.addWidget(out_dir)
self.show()
return out_path
#Sets the Style of the Window   
def style_choice(self, text):
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create(text))

def file_open(self):
name = QtGui.QFileDialog.getExistingDirectory()
#self, "Choose Directory", "")
print('path opened: {}'.format(name)) #Prints names
return name

def close_app(self):
choice = QtGui.QMessageBox.question(self, 'Are you sure?',
'Do you really want to exit the program?',
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if choice == QtGui.QMessageBox.Yes:
print('Exiting Program Now!')
sys.exit()
else:
pass

def using_path():
try:
directory = 0
print("Returned path is: {}".format(directory))
return 
except:
print('... progress bar failed')


def mainWindow():
app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())

if __name__ == '__main__':
mainWindow()

我无法获取输出路径,确认按钮不起作用或执行任何操作。

我感谢任何可以提供的帮助。谢谢!

将路径分配给self.path,然后在app.exec_()后获得GUI.path,但您必须跳过sys.exit()

import sys
import os
import PyQt4
from PyQt4 import QtGui
class Window(QtGui.QWizard):
def __init__(self):
super().__init__()
self.path = '' # default value at start (useful if you don't select new directory)
#layout = QtGui.QBoxLayout(QtGui.QBoxLayout.TopToBottom) 
layout = QtGui.QVBoxLayout()
self.setLayout(layout)
btn_out_dir = QtGui.QPushButton("Output Directory", self)
btn_out_dir.clicked.connect(self.path_select)
layout.addWidget(btn_out_dir)
btn_confirm = QtGui.QPushButton("Confirm", self)
btn_confirm.clicked.connect(self.close)
layout.addWidget(btn_confirm)
def path_select(self):
self.path = QtGui.QFileDialog.getExistingDirectory()
if self.path:
print('[inside] path:', self.path)
else:
print('[inside] path: - not selected -')

app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
status_code = app.exec_()
print('[outside] path:', GUI.path)
#sys.exit(status_code)

相关内容

  • 没有找到相关文章

最新更新