下面是如何使對話框彈出使用「WindowStaysOnTopHint」,顯示自己凌駕於所有其他窗口一個PyQt的例子:如何使PySide窗口彈出其他窗口之上
#copied from: http://stackoverflow.com/questions/1925015/pyqt-always-on-top
import sys
from PyQt4 import QtGui, QtCore
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
的'WindowStaysOnTopHint'屬性似乎是讓窗口在所有其他窗口之上彈出的關鍵。但我不知道如何使用「WindowStaysOnTopHint」屬性來獲取「文件夾實用程序」窗口(在下面的腳本中)彈出,顯示在所有其他窗口之上。 我嘗試在self.initUI()附近插入幾個選項,如下所示,但沒有在「其他窗口」上方彈出「Folder Utility」窗口(在下面的腳本中)。
這裏的腳本:
import os
import sys
# Based on 'http://stackoverflow.com/questions/22363123/pyside-get-directory-selected', but with many changes I made
from PySide import QtCore
from PySide import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
#self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
#flags = QtCore.Qt.WindowFlags()# The line above prevented the
# window from appearing at all
#flags |= QtCore.Qt.WindowStaysOnTopHint # This line and the line
# above had no effect.
self.initUI()
self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
def initUI(self):
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')
# EditText Field
labelNuDirName = QtGui.QLabel('New Folder Name:', self)
labelNuDirName.move(15, 10)
self.etNuDirName = QtGui.QLineEdit('', self)
self.etNuDirName.resize(self.etNuDirName.sizeHint())
self.etNuDirName.move(110, 7)
# Folder Browser
lbBroswer = QtGui.QLabel('Directory:', self)
lbBroswer.move(15, 40)
self.etBrowser = QtGui.QLineEdit('', self)
self.etBrowser.resize(210,20)#width, height https://srinikom.github.io/pyside-docs/PySide/QtGui/QWidget.html#PySide.QtGui.PySide.QtGui.QWidget.resize
self.etBrowser.move(90, 37)
self.etBrowser.setEnabled(0)
# self.etBrowser.isReadOnly = 0
btnBrowse = QtGui.QPushButton('...', self)
btnBrowse.setToolTip('Select directory ...')
btnBrowse.resize(30,20)
btnBrowse.move(305, 37)
btnBrowse.clicked.connect(self.selectDirectory)
# Button UI
btn = QtGui.QPushButton('Create Folder', self)
btn.setToolTip('This creates the folders.')
btn.resize(btn.sizeHint())
btn.move(5, 60)
btn.clicked.connect(self.generateFolders)
###Folder Selected Show
# label_NuDirSelected = QtGui.QLabel('', self)
# self.etNuDirName.resize(210,20)#width, height https://srinikom.github.io/pyside-docs/PySide/QtGui/QWidget.html#PySide.QtGui.PySide.QtGui.QWidget.resize
# lbBroswer.move(5, 80)
self.resize(350, 150)
self.center()
self.setWindowTitle('Folder Utility')
self.show()
def center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def selectDirectory(self):
dialog = QtGui.QFileDialog
selected_directory = dialog.getExistingDirectory(None,
title,
startingDir,
QtGui.QFileDialog.ShowDirsOnly)
# Use the selected directory...
self.etBrowser.setText(selected_directory)
print 'selected_directory:', selected_directory
# txt4__label_NuDirSelected = 'Directory you selected = "' + selected_directory + '"'
# self.label_NuDirSelected.setText(txt4__label_NuDirSelected)
def generateFolders(self):
existing_directory = self.etBrowser.text()
NuDirName = self.etNuDirName.text()
filePath = str(existing_directory) + os.sep + str(NuDirName)
if not os.path.exists(filePath):
os.makedirs(filePath)
if os.path.exists(filePath):
print 'Successfully Created Folders!'
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
靜態函數使用本機對話框,該對話框無法控制。使用'QFileDialog'構造函數[創建非本地對話框](https://doc.qt.io/qt-4.8/qfiledialog.html#details)。 – ekhumoro
感謝您提供該非常有用的網址。我從你的迴應推斷,我的長代碼示例問題中的窗口是一個「靜態函數」窗口,而「WindowStaysOnTopHint」不適用於這樣的窗口。你知道在哪裏可以找到如何在所有其他窗口之上彈出「靜態函數」窗口?而且,我在哪裏可以找到有關「靜態函數」窗口的信息?並再次感謝您的慷慨幫助。 – user235218
正如我所說:沒有辦法控制本地(即非Qt)對話框。 – ekhumoro