Hello

Quick question: Does anyone know why my window doesn't show frame?

Thank
Mark

import sys
from PySide import QtCore, QtGui
  
class UI_CMDTester:
    def __init__(self):
        self.__app = None
        self.__win = None
        
        
        
        
    def init(self,    \
             w_title, \
             w_width, \
             w_height):
        
        self.__app = QtGui.QApplication(sys.argv)
        self.__create_win(w_title, w_width, w_height)
        
        sys.exit(self.__app.exec_())
        
    def __create_win(self, w_title, w_width, w_height):
        
        self.__win = QtGui.QWidget()
        self.__win.resize(w_width, w_height)
        self.__win.setWindowTitle(w_title)
        
        frame = QtGui.QFrame()
        frame.setLineWidth(1)
        frame.setFrameRect(QtCore.QRect(200, 200, 300, 300))
        
        layout = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight)
        layout.addWidget(frame)
        
        self.__win.setLayout(layout)
        self.__win.show()
        

def main():
    obj_ct = UI_CMDTester()
    obj_ct.init("Window", 800, 600)        

if __name__ == "__main__":
    main()

Recommended Answers

All 8 Replies

I am guessing that the window doesn't contain anything visible, just containers. (This just displays a button.)

import sys
from PyQt4 import QtCore, QtGui
 
class UI_CMDTester:
    def __init__(self):
        self.__app = None
        self.__win = None
        
        
        
        
    def init(self,    \
             w_title, \
             w_width, \
             w_height):
        
        self.__app = QtGui.QApplication(sys.argv)
        self.__create_win(w_title, w_width, w_height)
        
        sys.exit(self.__app.exec_())
        
    def __create_win(self, w_title, w_width, w_height):
        
        self.__win = QtGui.QWidget()
        self.__win.resize(w_width, w_height)
        self.__win.setWindowTitle(w_title)
        
        close_button = QtGui.QPushButton("&Quit")

        layout = QtGui.QGridLayout()
        layout.addWidget(close_button, 1, 0)
        self.__win.setLayout(layout)

        self.__win.show()
        

if __name__ == "__main__":
    obj_ct = UI_CMDTester()
    obj_ct.init("Window", 800, 600)

Not sure where you found that awful coding style. I would recommend the following style ...

# explore QFrame()

from PySide.QtCore import *
from PySide.QtGui import *

class FrameTester(QWidget):
    def __init__(self, title, width, height, parent=None):
        # create the window (this will be instance self)
        QWidget.__init__(self, parent)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(100, 150, width, height)
        self.setWindowTitle(title)
        self.make_frame()
    
    def make_frame(self):
        frame = QFrame(self)
        frame.setLineWidth(3)
        frame.setFrameStyle(QFrame.Box|QFrame.Sunken)
        
        # this will not have the effect you hope for
        #frame.setFrameRect(QRect(10, 10, 20, 20))          
       
        layout = QBoxLayout(QBoxLayout.LeftToRight)
        layout.addWidget(frame)
        
        self.setLayout(layout)


# create the Qt Application
app = QApplication([])

title = "Window"
width = 800
height = 600
tester = FrameTester(title, width, height)
tester.show()

# run the main Qt event loop
app.exec_()

In your code you had the frame, but it blended in with the window background.

Thanks to wooee,
vegaseat (I'm very new into Python, just started to learn last Monday. Still lots to learn :-)) I took your advise on coding and also done some research on the PySide docs. Here is another problem:

I want the top 2 frames have a hight and lower the height of bottom frame. I tried to set QRect to frame but that didn't work, and advice?

import sys
from PySide import QtCore, QtGui

class Window(QtGui.QWidget):
    __WND_WIDTH  = 800
    __WND_HEIGHT = 600
    __WND_TITLE  = "Window"

    def __init__(self, parent = None):
        self.app = QtGui.QApplication(sys.argv)
        self.app.setStyle(QtGui.QStyleFactory.create("Cleanlooks"))
        QtGui.QWidget.__init__(self, parent)
        self.__wnd_init()
        self.show()
        sys.exit(self.app.exec_())


    def __wnd_init(self):
        self.setWindowTitle(self.__WND_TITLE)
        self.resize(self.__WND_WIDTH, self.__WND_HEIGHT)

        self.__wnd_center()
        self.__wnd_layout()


    def __wnd_center(self):
        scrn = QtGui.QDesktopWidget().screenGeometry()
        self.move((scrn.width()  - self.__WND_WIDTH)  / 2, \
                  (scrn.height() - self.__WND_HEIGHT) / 2)
        del scrn # Small Cleanup


    def __get_frame(self, f_shape):
        frame = QtGui.QFrame(self)
        frame.setFrameShape(f_shape)
        return frame


    def __wnd_layout(self):
        h_box   = QtGui.QHBoxLayout(self)
        h_split = QtGui.QSplitter(QtCore.Qt.Horizontal)
        v_split = QtGui.QSplitter(QtCore.Qt.Vertical)

        # Top Left / Top Right, Frames
        h_split.addWidget(self.__get_frame(QtGui.QFrame.Box))
        h_split.addWidget(self.__get_frame(QtGui.QFrame.Box))

        # Bottom Frame
        v_split.addWidget(h_split); del h_split
        v_split.addWidget(self.__get_frame(QtGui.QFrame.Box))

        h_box.addWidget(v_split); del v_split
        self.setLayout(h_box)
        

        


def main():
    Window();

if __name__ == "__main__":
    main()

Something like this I recommend a QGridLayout() ...

# explore multiple QFrame() in a QGridLayout()

from PySide.QtCore import *
from PySide.QtGui import *

class FrameTester(QWidget):
    def __init__(self, title, width, height, parent=None):
        # create the window (this will be instance self)
        QWidget.__init__(self, parent)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(100, 150, width, height)
        self.setWindowTitle(title)
        self.make_frame()
    
    def make_frame(self):
        frame1 = QFrame(self)
        frame1.setLineWidth(3)
        frame1.setFrameStyle(QFrame.Box|QFrame.Sunken)
        
        frame2 = QFrame(self)
        frame2.setLineWidth(3)
        frame2.setFrameStyle(QFrame.Box|QFrame.Sunken)        

        frame3 = QFrame(self)
        frame3.setLineWidth(3)
        frame3.setFrameStyle(QFrame.Box|QFrame.Sunken)
        
        grid = QGridLayout()
        grid.setSpacing(10)
        # addWidget(QWidget, row, column, rowSpan, columnSpan)
        # span 2 rows and 1 column each
        grid.addWidget(frame1, 1, 1, 2, 1)
        grid.addWidget(frame2, 1, 2, 2, 1)
        # span 1 row and 2 columns
        # note that you occupy row 3 now
        grid.addWidget(frame3, 3, 1, 1, 2)
       
        self.setLayout(grid)
    

# create the Qt Application
app = QApplication([])

title = "3 frames in a grid layout"
width = 800
height = 600
tester = FrameTester(title, width, height)
tester.show()

# run the main Qt event loop
app.exec_()

Again, I don't know where you are picking up your outdated coding style, seems from the very beginning of PyQT. It makes it hard for normal mortals to follow and help you.

vegaseat, I have another question:
Do you know a way to get the size of QPushButton (Width, Height)?

Thanks again for all your help.


Mark

# test PySide widgets
# get widget's size

from PySide.QtCore import *
from PySide.QtGui import *

app = QApplication([])

# ----- start your widget test code ----

label = 'x'*20
button = QPushButton(label)
width = button.sizeHint().width()
height = button.sizeHint().height()

button.show()

print("button width=%d  height=%d (in pixels)" % (width, height))

# ---- end of widget test code -----

app.exec_()

Thanks everyone for helping me out.

I have another question (QtCore.QProcess):
Here how my program works: I have button (QtGui.QPushButton) when it gets clicked, my program should call my C compiler (GCC) and compile the source code. This works perfectly when I had no GUI (Command Line) using subprocess.

Now I'm trying to use QProcess to do a same thing. My MainWindow class has one instance of QProcess and once instance of QtGui.QTextEdit

on __init__

self.process = QtCore.QProcess(self)
self.textEdit = QTextEdit(self)
self.textEdit.move(x, y)

and more

now my button (Which is not in the above code), called btn_run has an event the when it gets clicked it start the process

args = ["-o", "myfile", "myfile.c"]
self.process.start("gcc", args)

Now what I want to do is to get the outputs that generated by gcc on STDOUT, STDERR from QProcess to show them in QTextEdit line by line (as gcc puts something on STOUT or STDERR I show that on my QTextEdit)

Here are two problems that I have, First my UI freezes, meaning that I cant press anything else with my UI and I get no output on my QTextEdit. (I used readLine() function which is from QIODevice, which is a parent class of QProcess).

Any suggestion?

Thanks again.


--
Mark

Please start a new thread with a more specific title.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.