Skip to content

Instantly share code, notes, and snippets.

@MrGibus
Last active May 3, 2020 15:42
Show Gist options
  • Select an option

  • Save MrGibus/be7917a4dbb4eae0f8c4522fc241ac69 to your computer and use it in GitHub Desktop.

Select an option

Save MrGibus/be7917a4dbb4eae0f8c4522fc241ac69 to your computer and use it in GitHub Desktop.
PySide2 pushButton and lineEdit interaction with UI file and notes
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'mainwindow.ui'
##
## Created by: Qt User Interface Compiler version 5.14.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide2.QtCore import (QCoreApplication, QMetaObject,
QRect)
from PySide2.QtWidgets import *
# noinspection PyAttributeOutsideInit
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(800, 126)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.verticalLayout = QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.pushButton = QPushButton(self.centralwidget)
self.pushButton.setObjectName(u"pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.lineEdit = QLineEdit(self.centralwidget)
self.lineEdit.setObjectName(u"lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 800, 21))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.pushButton.setText(QCoreApplication.translate("MainWindow", u"PushButton", None))
# retranslateUi
# The my_ui file is created from running pyside2-uic command on a created .ui file > see below
# CMD: pyside2-uic mainwindow.ui > my_ui.py
# The ui file has a button (pushButton) and text box (lineEdit) created in Qt Designer with names left as default
# This shows how to access the button and textbox
import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from my_ui import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__() # Inherit Parent Classes
self.setupUi(self) # Defines the UI elements
self.btn_mgr() # My Button Manager to connect buttons and functions
self.show() # Show the window when class is created
def btn_mgr(self): # Button manager: Currently only manages one button
self.pushButton.clicked.connect(btn_fn) # The clicked.connect method connects the click with the function
def btn_fn():
my_input = ui.lineEdit.text() # Assign the text in the box to a variable
print(my_input) # Then we have access to that data
ui.lineEdit.clear() # Or we can enter something into the box -Clear existing data first
ui.lineEdit.insert('this is base function') # Write new data
if __name__ == "__main__": # If this is the main application
app = QApplication(sys.argv) # Create the application object to control the main event loop
ui = MainWindow() # Create the UI object (the show method is in the class)
app.exec_() # Start event loop via exec_ method which returns 1 while running
# We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your
# application's main() function. - Qt Docs [ i.e don't rely on sys.exit(app.exec_()) ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment