我有一个小应用程序来运行一些命令并在QTextBrowser中重定向输出。当我在控制台小部件中使用按钮和命令行时,它工作正常。但是也希望在侧面使用按钮。当我从侧面小部件调用该函数时,它运行命令,但我没有在QTextBrowser中获得输出。
请参阅下面的代码示例。非常感谢。
#!/usr/bin/env python
import sys
from PySide import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
# Add the Graphics widget
self.graphicsViewWidget = GraphicsViewWidget(self)
self.graphicsScene = self.graphicsViewWidget.scene()
self.setCentralWidget(self.graphicsViewWidget)
# Create side widget
self.sideDock = QtGui.QDockWidget()
self.sideDock.setAllowedAreas(QtCore.Qt.RightDockWidgetArea | QtCore.Qt.LeftDockWidgetArea)
self.sideDock.setWindowTitle("SideWidget")
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.sideDock)
self.sideWidget = SideWidget(self)
self.sideDock.setWidget(self.sideWidget)
# Create console widget
self.consoleDock = QtGui.QDockWidget()
self.consoleDock.setAllowedAreas(QtCore.Qt.RightDockWidgetArea | QtCore.Qt.LeftDockWidgetArea)
self.consoleDock.setWindowTitle("Console")
self.addDockWidget(QtCore.Qt.BottomDockWidgetArea, self.consoleDock)
self.consoleWidget = ConsoleWidget(self)
self.consoleDock.setWidget(self.consoleWidget)
# Create menu
fileMenu = self.menuBar().addMenu("&File")
fileMenu.addAction(QtGui.QAction("&Quit...", self, shortcut="Ctrl+Q", triggered=self.close))
class GraphicsViewWidget(QtGui.QGraphicsView):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent)
class SideWidget(QtGui.QWidget, ):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.mainLayout = QtGui.QVBoxLayout(self)
# Buttons
self.execute_button = QtGui.QPushButton(self)
self.execute_button.setText('Execute')
self.execute_button.setFixedHeight(25)
# Add Buttons to widget
self.mainLayout.addWidget(self.execute_button)
# Button actions
self.execute_button.pressed.connect(self.execute_button_pressed)
def execute_button_pressed(self):
print('execute_button_pressed')
##########################################
# want to run commands from here
##########################################
cnsl=ConsoleWidget()
cnsl.run_command()
cnsl=RunCommands(self)
cnsl.run_command()
#RunCommands(self)
class ConsoleWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.mainLayout = QtGui.QVBoxLayout(self)
# label
self.label = QtGui.QLabel('Command Line', self)
# commandline
self.commandline = QtGui.QLineEdit(self)
self.commandline.setFixedHeight(25)
self.commandline.setToolTip('Run commands here')
# The upper layout holds label and commandline
upperLayout = QtGui.QHBoxLayout()
upperLayout.addWidget(self.label)
upperLayout.addWidget(self.commandline)
# buttons
self.dummy_button = QtGui.QPushButton(self)
self.dummy_button.setText('Run Dummy')
self.dummy_button.setToolTip('For testing')
self.dummy_button.setFixedHeight(25)
# Textbrowser
#self.textbrowser = QtGui.QTextBrowser(self)
self.textbrowser = QtGui.QTextEdit(self) # for typing in
# The lower layout holds the textbrowser
lowerLayout = QtGui.QVBoxLayout()
lowerLayout.addWidget(self.dummy_button)
lowerLayout.addWidget(self.textbrowser)
# Add layouts to mainlayout
self.mainLayout.addLayout(upperLayout)
self.mainLayout.addLayout(lowerLayout)
self.setLayout(self.mainLayout)
# Button actions
self.dummy_button.clicked.connect(self.run_command)
self.commandline.returnPressed.connect(self.run_commandline)
def run_command(self):
print('run_command')
command='cmd.exe /c echo test'
print('Self ',self)
print('Command',command)
self.textbrowser.append('test')
# err and out
##self.runner.readyReadStandardError.connect(self.ErrInfo)
##self.runner.readyReadStandardOutput.connect(self.OutInfo)
# Run command
##self.runner.start(command)
def run_commandline(self):
command_text = str(self.commandline.text())
self.runner = QtCore.QProcess()
# Make sure newInfo gets all output
self.runner.readyReadStandardError.connect(self.ErrInfo)
self.runner.readyReadStandardOutput.connect(self.OutInfo)
# Run the command
command='cmd.exe /c ' + command_text
#print(command)
self.runner.start(command)
# clearing commandline
self.commandline.clear()
def ErrInfo(self):
errString = str(self.runner.readAllStandardError())
print('errString', errString, end=" ")
self.textbrowser.append(errString)
def OutInfo(self):
outString = str(self.runner.readAllStandardOutput())
print('outString', outString, end=" ")
#cnsl=ConsoleWidget()
self.textbrowser.append(outString)
class RunCommands(QtGui.QTextBrowser):
def __init__(self, parent):
print('RunCommands')
QtGui.QTextBrowser.__init__(self, parent)
# Textbrowser
#self.textbrowser = ConsoleWidget().textbrowser
self.run_command()
#self.textbrowser.append('test2')
def run_command(self):
print('run_command')
command='cmd.exe /c echo test'
command='cmd.exe /c notepad'
print('Self ',self)
print('Command',command)
ConsoleWidget().textbrowser.append('test2')
self.runner = QtCore.QProcess(self)
# err and out
self.runner.readyReadStandardError.connect(self.ErrInfo)
self.runner.readyReadStandardOutput.connect(self.OutInfo)
# Run command
self.runner.start(command)
def run_commandline(self):
command_text = str(self.commandline.text())
self.runner = QtCore.QProcess()
# err and out
self.runner.readyReadStandardError.connect(self.ErrInfo)
self.runner.readyReadStandardOutput.connect(self.OutInfo)
# Run command
command='cmd.exe /c ' + command_text
#print(command)
self.runner.start(command)
# clearing commandline
self.commandline.clear()
def ErrInfo(self):
errString = str(self.runner.readAllStandardError())
print('errString', errString, end=" ")
self.textbrowser.append(errString)
def OutInfo(self):
outString = str(self.runner.readAllStandardOutput())
print('outString', outString, end=" ")
self.textbrowser.append(outString)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
这是一个关于您在何处创建窗口小部件以及如何访问它们的问题。
您的MainWindow
有两个子窗口小部件:sideWidget
和consoleWidget
。
然后,在SideWidget.execute_button_pressed
中,您创建另一个 ConsoleWidget
。这个新的小部件在你的布局中没有任何地方,所以你只是看不到它。
要将MainWindow.sideWidget
(点击按钮)的输入连接到MainWindow.consoleWidget
的输出(运行命令和打印文本),您只需在课程MainWindow
中执行此操作。
像这样:
class MainWindow(QtGui.QMainWindow):
def __init__(self,parent=None):
...
self.sideWidget.execute_button.clicked.connect(self.execute)
...
def execute_function(self):
print('execute_button_pressed')
self.consoleWidget.run_command()
或者只是一行:
self.sideWidget.execute_button.clicked.connect(self.consoleWidget.run_command)