2015-07-19 81 views
0

我正在制作一个小脚本,其中xterm嵌入在一个小GUI中。当我运行我的尝试时,终端小部件的边缘被切断,我不知道为什么。我怎样才能显示完整的终端?PyQt小部件边缘切断

#!/usr/bin/env python 

import sys 
from PyQt4 import QtCore, QtGui 

class embedded_terminal(QtGui.QWidget): 

    def __init__(self): 

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

     # layout 

     self.layout = QtGui.QVBoxLayout(self) 

     # 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) 
      self.layout.addWidget(button) 

     # terminal 

     self.terminal = QtGui.QWidget(self) 
     self.layout.addWidget(self.terminal) 
     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_()) 

回答

2

这是因为terminal widget是大于xterm窗口。您可以通过

self.terminal = QtGui.QTextBrowser() 
self.terminal.setFrameStyle(QtGui.QTextEdit.DrawWindowBackground) 

更换

self.terminal = QtGui.QWidget() 

,看看你的terminal的实际大小。我已经找到了解决办法是设置terminal接近xterm窗口大小初始大小:

self.terminal.setFixedSize(730, 440) 
+0

嗨@Alexander Lutsenko的。感谢您的解决方案,该解决方案运行良好,并为您解释如何考虑终端的大小。 – d3pd

1

QWidget有一个相关的QVerticalLayout,所以要根据文件似乎在默认情况下它延伸仅垂直,所以你应该手动设置大小的策略。尝试是这样的:

self.terminal.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)