Skip to content

Instantly share code, notes, and snippets.

@ewerybody
Forked from itarozzi/main.py
Last active January 28, 2021 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewerybody/27d40055d47374c4b12d1e14874e36a5 to your computer and use it in GitHub Desktop.
Save ewerybody/27d40055d47374c4b12d1e14874e36a5 to your computer and use it in GitHub Desktop.
Frame
# built-in imports should be at the top
# but sys is not even needed! See last block.
# import sys
# Module imports are preferred! Unneeded items should not be imported
from PySide2 import QtCore, QtWidgets
# numbers that are used multiple times or are key to the functionality
# should be in 1 place so one doesn't need to dig for the settings.
SIZE_SMALL = 70
SIZE_BIG = 200
# human reaction time is around 200ms, I'd stick to that for such transitions.
DURATION = 200
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Conversor de arquivos')
# x, y, w, h -> esquerda, topo, largura, altura
self.setGeometry(300, 100, 900, 600)
# if you set both width & height, use setMinimumSize
self.setMinimumSize(700, 500)
# you can pass self so you don't need setLayout
self.vbox_main = QtWidgets.QVBoxLayout(self)
self.hbox_main = QtWidgets.QHBoxLayout()
self.hbox_main.setContentsMargins(0, 0, 0, 0)
self.hbox_main.stretch(1)
self.button_toggle = QtWidgets.QPushButton('TOGGLE')
self.button_toggle.setMaximumSize(100, 40)
self.button_toggle.clicked.connect(self.toggle)
self.frame_left_menu = QtWidgets.QFrame()
self.frame_left_menu.setStyleSheet('background-color: green')
# Max size 16777215 is default, you just need the width to be set
# self.frame_left_menu.setMaximumSize(QtCore.QSize(70, 16777215))
self.frame_left_menu.setMaximumWidth(SIZE_SMALL)
self.vbox_menu_left = QtWidgets.QVBoxLayout(self.frame_left_menu)
self.vbox_menu_left.setSpacing(5)
self.frame = QtWidgets.QFrame()
self.frame.setStyleSheet('background-color: yellow')
# Why?
# self.frame.setMinimumWidth(16777215)
# Add objects to layouts
# I'd use a loop for these
for i in range(1, 5):
button = QtWidgets.QPushButton(f'BTN{i}')
self.vbox_menu_left.addWidget(button)
self.hbox_main.addWidget(self.frame_left_menu)
self.hbox_main.addWidget(self.frame)
self.vbox_main.addWidget(self.button_toggle)
self.vbox_main.addLayout(self.hbox_main)
# create the anim object once, just trigger it when needed in toggle()
self.animation = QtCore.QPropertyAnimation(self.frame_left_menu, b'minimumWidth')
self.animation.setDuration(DURATION)
self.animation.setEasingCurve(QtCore.QEasingCurve.InOutCubic)
def toggle(self):
current_width = self.frame_left_menu.width()
if current_width == SIZE_SMALL:
# use 1 naming style
width_extended = SIZE_BIG
else:
width_extended = SIZE_SMALL
self.animation.setStartValue(current_width)
self.animation.setEndValue(width_extended)
self.animation.start()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
widget = MyWidget()
# Have only 1 spot where the size is set!
# This would even set it smaller than the set minimum size!
# widget.resize(400, 300)
widget.show()
# just use QApplication.exec_ here. No need for sys.exit. happens anyway.
app.exec_()
@CoutinhoElias
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment