2015-07-19 46 views
0

我正在創建一個接口,該接口由一個嵌入在PyQt GUI中的終端組成,其中包含一些按鈕,用於在該終端中運行命令。當我運行我的代碼時,我認爲GUI的一部分已經正確創建,但終端小部件被隱藏了。我怎樣才能防止這種情況發生?如何防止PyQt小部件被遮蓋?

import sys 
from PyQt4 import QtCore, QtGui 

class embedded_terminal(QtGui.QWidget): 

    def __init__(self): 

     QtGui.QWidget.__init__(self) 
     self._processes = [] 
     self.resize(800, 800) 

     # set grid layout 

     grid = QtGui.QGridLayout() 

     # layout group for buttons 

     group_buttons = QtGui.QGroupBox() 
     vbox = QtGui.QVBoxLayout() 
     # define buttons 
     button_list = self.command_button(
      title = "ls", 
      command = "ls" 
     ) 
     button_terminate = QtGui.QPushButton("terminate") 
     button_terminate.clicked.connect(lambda: self.terminate()) 
     # style buttons and add buttons to layout 
     buttons = [] 
     buttons.append(button_list) 
     buttons.append(button_terminate) 
     for button in buttons: 
      self.set_button_style(button) 
      vbox.addWidget(button) 
     vbox.addStretch(1) 
     group_buttons.setLayout(vbox) 

     # layout group for terminal 

     group_terminal = QtGui.QGroupBox() 
     group_terminal.setLayout(vbox) 
     vbox = QtGui.QVBoxLayout() 
     # terminal 
     self.terminal = QtGui.QWidget(self) 
     vbox.addWidget(self.terminal) 
     vbox.addStretch(1) 
     group_terminal.setLayout(vbox) 

     # add layout groups to grid layout 
     grid.addWidget(group_buttons, 0, 0) 
     grid.addWidget(group_terminal, 0, 1) 
     self.setLayout(grid) 

     self.start_process(
      "xterm", 
       [ 
        "-fn", 
        "-misc-fixed-*-*-*-*-18-*-*-*-*-*-*-*", 
        "-into", 
        str(self.terminal.winId()), 
        "-e", 
        "tmux", 
        "new", 
        "-s", 
        "session1" 
       ] 
     ) 

    def start_process(
     self, 
     program, 
     options 
     ): 
     child = QtCore.QProcess() 
     self._processes.append(child) 
     child.start(program, options) 

    def run_command(
     self, 
     command = "ls" 
     ): 
     program = "tmux" 
     options = [] 
     options.extend(["send-keys", "-t", "session1:0"]) 
     options.extend([command]) 
     options.extend(["Enter"]) 
     self.start_process(program, options) 

    def command_button(
     self, 
     title = None, 
     command = None 
     ): 
     button = QtGui.QPushButton(title) 
     button.clicked.connect(lambda: self.run_command(command = command)) 
     return button 

    def set_button_style(
     self, 
     button 
     ): 
     # Set button style. 
     button.setStyleSheet(
      """ 
      color: #{color1}; 
      background-color: #{color2}; 
      border: 1px solid #{color1}; 
      """.format(
       color1 = "3861aa", 
       color2 = "ffffff" 
      ) 
     ) 
     # Set button dimensions. 
     button.setFixedSize(
      300, 
      60 
     ) 

    def terminate(self): 
     program = "tmux" 
     options = [] 
     options.extend(["send-keys", "-t", "session1:0"]) 
     options.extend(["killall tmux"]) 
     options.extend(["Enter"]) 
     self.start_process(program, options) 
     QtGui.QApplication.instance().quit() 

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    main = embedded_terminal() 
    main.show() 
    sys.exit(app.exec_()) 

回答

1

對不起我的英語。

您有一個錯誤:在創建它之前添加一個佈局。因此,您附加到group_terminalgroup_buttons相同的vbox

# layout group for terminal 
group_terminal = QtGui.QGroupBox() 
group_terminal.setLayout(vbox) # <- old one 
vbox = QtGui.QVBoxLayout() 

此外,Qt對您的xterm窗口的大小一無所知。好像你必須手動指定它。此代碼正常工作:

group_terminal = QtGui.QGroupBox() 
vbox = QtGui.QVBoxLayout() 
group_terminal.setLayout(vbox) 

# terminal 
self.terminal = QtGui.QWidget() 
self.terminal.setFixedSize(730, 440)