PyQt小部件边缘被切断

时间:2015-07-19 18:51:41

标签: python terminal pyqt

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

#!/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 个答案:

答案 0 :(得分:2)

那是因为terminal窗口小部件大于xterm窗口。你可以替换

self.terminal = QtGui.QWidget()

通过

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

并查看terminal的实际尺寸。我发现的解决方案是将terminal初始大小设置为接近xterm窗口大小:

self.terminal.setFixedSize(730, 440)

答案 1 :(得分:1)

您的QWidget有关联的QVerticalLayout,因此根据文档,默认情况下它似乎只是垂直延伸,因此您应手动设置尺寸政策。尝试这样的事情:

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