【发布时间】:2019-08-20 07:54:18
【问题描述】:
我正在尝试创建一个弹出向导,让用户选择一个目录路径,将该路径保存到一个变量中,然后返回该路径以在另一个脚本中使用。
我对 Python 编码比较陌生,我只是在玩。我尝试了 Tkinter,但不喜欢图书馆的外观/感觉。虽然 PyQt 总体上更好用。我尝试以几种不同的方式设置按钮,但收效甚微。我尝试使用 YouTube 视频中显示的“self”和“self.variable”,但我不熟悉它的帮助和使用方式。
When there is a path selected, it will print the output, but it does not seem to be returning the path to use, and if it is, I am unaware of how it is doing so and how to use it in other脚本。
附带说明:我在 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()
我无法获得输出路径,并且确认按钮不起作用或执行任何操作。
感谢您提供的任何帮助。谢谢!
【问题讨论】:
-
__init__无法返回值 - 它在您创建实例时执行,但代码返回类的实例而不是来自__init__的值 -
为类中的变量分配路径 - 即
self.path并在app.exec()之后将其获取为GUI.path。但删除sys.exit()
标签: python directory path pyqt4