About PyQt6: How to modify a button?

Hello everyone,

This is my first question in this forum. My Python level is probably between a beginner and a pre-intermediate. My major is not related to computer sciences; but I really like coding and I’m trying to develop myself, trying to code in my free times. I can do simple codes and write codes for special numbers (e.g. Harshad numbers, Kaprekar numbers etc.) I want to make simple applications/games, such as a calculator or tetris or blackjack etc. I can write codes as script but I need to learn GUI.

Currently, I’m trying to make a black jack game. Now, I have a “Shuffle” button. How can I modify it? I would like to change it’s appearance with a png I’ve downloaded a file from the internet (I’ve put the file in the same directory with the game file). Following code is the part of my code:

def widgetcreation(self):
btn = QPushButton(“SHUFFLE”, self)
btn.setStyleSheet(
“background-color: red;”
“font-family: times;”
“font-size: 20px;”
)
btn.setGeometry(700, 400, 100, 100)

How can I choose different colours, size and fonts? Is there a list for it?

I’m sure these are newbie questions but hoping your help.

Thanks in advance.

The indent on your code was lost but i can see what you are doing.

For the CSS details see Qt Style Sheets Reference | Qt Widgets 6.4.1

For adding an icon to a qpushbutton see QPushButton Class | Qt Widgets 6.4.1

You will need to refer to the Qt docs to program PyQt6.
The docs are for C++ but have obvious mappings to python.

1 Like

Hello again,

I made some progress and I’m able to add a label (png) and some buttons. Where I’m stuck right now is to draw a rectangle. I think it is supposed to be pretty easy but somehow I couldn’t manage program to display it on the screen. Here is the rectangle code (self.painter = QPainter(self) is under the class Main Window):

def bank(self):
self.painter.setPen(QPen(Qt.GlobalColor.black, 5, Qt.PenStyle.SolidLine))
self.painter.setBrush(QBrush(Qt.GlobalColor.blue, Qt.BrushStyle.DiagCrossPattern))
self.painter.drawRect(600, 350, 200, 200)
self.painter.end()

When I run the code everything works fine (buttons, labels etc. are all ok) except I can’t draw the rectangle.

Is the above code correct to draw a rectangle in PyQt6? Where am I doing wrong?

Thanks in advance <3