How do I alter the following code to make it print whatever is written in line edit widget when 'OK' button is pressed? The current version returns "'Example' object has no attribute 'textbox'" error.
import sys
from PyQt5.QtWidgets import QApplication, QWidget,QPushButton,QLineEdit, QHBoxLayout, QLabel, QVBoxLayout
from PyQt5.QtGui import QIcon
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
label = QLabel('Keyword')
button = QPushButton('OK')
textbox = QLineEdit()
hbox = QHBoxLayout()
hbox.addWidget(label)
hbox.addWidget(textbox)
hbox.addWidget(button)
vbox = QVBoxLayout()
vbox.addLayout(hbox)
vbox.addStretch(1)
button.clicked.connect(self.button_clicked)
self.setLayout(vbox)
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Icon')
self.setWindowIcon(QIcon('web.png'))
self.show()
def button_clicked(self):
print(self.textbox.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
`
if you want that a variable can be accessed in all parts of the class as in your case is the button_clicked method you must make it a member of the class for it you must use self when you create it.
class Example(QWidget):
[...]
def initUI(self):
label = QLabel('Keyword')
button = QPushButton('OK')
self.textbox = QLineEdit() # change this line
hbox = QHBoxLayout()
hbox.addWidget(label)
hbox.addWidget(self.textbox) # change this line
Related
I want to be able to move a layout to another layout based on a user input. I have the following code which does not appear to work for me. If I switch lines 31 and 34 so that they operate on the widget rather than the layout then I get the expected behaviour but I am hoping to operate on all widgets within a layout by just moving the layout.
import sys
from PyQt5.QtWidgets import QPushButton, QWidget, QHBoxLayout, QLabel, QApplication, QVBoxLayout
class b(QWidget):
def __init__(self, name):
super(b, self).__init__()
self.layout = QVBoxLayout(self)
lbl_1 = QLabel(name)
self.layout.addWidget(lbl_1)
class a(QWidget):
def __init__(self):
super(a, self).__init__()
self.layout = QHBoxLayout(self)
self.widget_1 = b('widget 1')
self.widget_2 = b('widget 2')
self.layout.addWidget(self.widget_1)
self.layout.addWidget(self.widget_2)
self.button_layout = QHBoxLayout()
self.move_layout = QPushButton('Move to other layout')
self.move_layout.clicked.connect(lambda: self.move_button())
self.button_layout.addWidget(self.move_layout)
self.widget = 'widget_2'
self.widget_2.layout.addLayout(self.button_layout)
def move_button(self):
if self.widget == 'widget_2':
self.widget_1.layout.addLayout(self.button_layout)
self.widget = 'widget_1'
else:
self.widget_2.layout.addLayout(self.button_layout)
self.widget = 'widget_2'
print('moved widget to {}'.format(self.widget))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = a()
window.show()
sys.exit(app.exec_())
Edit: to clarify, In the example above, the layout I want to move (self.button_layout) is a child layout of self.widget_2.layout. When I click the pushbutton, I want the self.button_layout to be set as a child layout of self.widget_1.layout. Essentially it will do what the code below does but using addLayout instead of addWidget.
import sys
from PyQt5.QtWidgets import QPushButton, QWidget, QHBoxLayout, QLabel, QApplication, QVBoxLayout
class b(QWidget):
def __init__(self, name):
super(b, self).__init__()
self.layout = QVBoxLayout(self)
lbl_1 = QLabel(name)
self.layout.addWidget(lbl_1)
class a(QWidget):
def __init__(self):
super(a, self).__init__()
self.layout = QHBoxLayout(self)
self.widget_1 = b('widget 1')
self.widget_2 = b('widget 2')
self.layout.addWidget(self.widget_1)
self.layout.addWidget(self.widget_2)
self.button_layout = QHBoxLayout()
self.move_layout = QPushButton('Move to other layout')
self.move_layout.clicked.connect(lambda: self.move_button())
self.button_layout.addWidget(self.move_layout)
self.widget = 'widget_2'
self.widget_2.layout.addLayout(self.button_layout)
def move_button(self):
if self.widget == 'widget_2':
self.widget_1.layout.addWidget(self.move_layout)
self.widget = 'widget_1'
else:
self.widget_2.layout.addWidget(self.move_layout)
self.widget = 'widget_2'
print('moved widget to {}'.format(self.widget))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = a()
window.show()
sys.exit(app.exec_())
The problem is that if a layout has a parent then it cannot be changed as the error message indicates:
QLayout::addChildLayout: layout "" already has a parent
One possible solution is to remove the parent:
def move_button(self):
self.button_layout.setParent(None)
if self.widget == "widget_2":
self.widget_1.layout.addLayout(self.button_layout)
self.widget = "widget_1"
else:
self.widget_2.layout.addLayout(self.button_layout)
self.widget = "widget_2"
print("moved widget to {}".format(self.widget))
Another alternative is to place the layout in a QWidget that is the container and that place it in the required layout:
class a(QWidget):
def __init__(self):
super(a, self).__init__()
layout = QHBoxLayout(self)
self.widget_1 = b("widget 1")
self.widget_2 = b("widget 2")
layout.addWidget(self.widget_1)
layout.addWidget(self.widget_2)
self.container = QWidget()
container_layout = QHBoxLayout(self.container)
button = QPushButton("Move to other layout")
button.clicked.connect(self.move_button)
container_layout.addWidget(button)
self.widget = "widget_1"
self.move_button()
def move_button(self):
if self.widget == "widget_2":
self.widget_1.layout.addWidget(self.container)
self.widget = "widget_1"
else:
self.widget_2.layout.addWidget(self.container)
self.widget = "widget_2"
print("moved widget to {}".format(self.widget))
This question already has answers here:
How do I assert the identity of a PyQt5 signal?
(2 answers)
Closed 2 years ago.
I've created a search engine in PyQt5, using the code below:
import sys
from PyQt5.QtWidgets import (
QWidget, QLineEdit, QLabel, QScrollArea, QMainWindow,
QApplication, QHBoxLayout, QVBoxLayout, QSpacerItem, QSizePolicy, QCompleter, QPushButton
)
from PyQt5 import QtCore
from PyQt5.QtCore import Qt
tlist = ['thing1', 'thing2', 'thing3', 'thing4']
class Label(QWidget):
def __init__(self, name):
super(Label, self).__init__()
self.name = name
self.lbl = QLabel(self.name)
self.lbl.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
self.btn = QPushButton("Preview")
self.btn.setMaximumSize(QtCore.QSize(100,100))
self.btn.clicked.connect(self.printsignal)
self.hbox = QHBoxLayout()
self.hbox.addWidget(self.lbl)
self.hbox.addWidget(self.btn)
self.setLayout(self.hbox)
def show(self):
for labels in [self, self.lbl]:
labels.setVisible(True)
def hide(self):
for labels in [self, self.lbl]:
labels.setVisible(False)
def printsignal(self):
print("clicked")
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__()
self.controls = QWidget()
self.controlsLayout = QVBoxLayout()
self.widgets = []
for name in tlist:
item = Label(name)
self.controlsLayout.addWidget(item)
self.widgets.append(item)
spacer = QSpacerItem(1, 1, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.controlsLayout.addItem(spacer)
self.controls.setLayout(self.controlsLayout)
self.scroll = QScrollArea()
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(self.controls)
self.searchbar = QLineEdit()
self.searchbar.textChanged.connect(self.update_display)
self.completer = QCompleter(tlist)
self.completer.setCaseSensitivity(Qt.CaseInsensitive)
self.searchbar.setCompleter(self.completer)
container = QWidget()
containerLayout = QVBoxLayout()
containerLayout.addWidget(self.searchbar)
containerLayout.addWidget(self.scroll)
container.setLayout(containerLayout)
self.setCentralWidget(container)
self.setGeometry(600, 100, 800, 600)
self.setWindowTitle('Search Engine')
def update_display(self, text):
for widget in self.widgets:
if text.lower() in widget.name.lower():
widget.show()
else:
widget.hide()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
The problem I have is, all the buttons share the same function and I don't know how to make them have different signals, as they are generated automatically. Basically, if I run the code it will show up like
this:
and when I press any of the buttons, it will print "clicked" (as in printsignal function). What I want is a different function for each button. Is there a way to do that?
Normally you can use self.sender().text() to get text from QButton which generated signal.
But because you create own widget Label with QButton and QLabel and you want text from label so you can get directly self.name
def printsignal(self):
print("clicked", self.name)
eventually self.lbl.text()
def printsignal(self):
print("clicked", self.lbl.text())
Working code.
I removed show(), hide() because you don't need it
import sys
from PyQt5.QtWidgets import (
QWidget, QLineEdit, QLabel, QScrollArea, QMainWindow,
QApplication, QHBoxLayout, QVBoxLayout, QSpacerItem, QSizePolicy, QCompleter, QPushButton
)
from PyQt5 import QtCore
from PyQt5.QtCore import Qt
tlist = ['thing1', 'thing2', 'thing3', 'thing4']
class Label(QWidget):
def __init__(self, name):
super().__init__()
self.name = name
self.lbl = QLabel(self.name)
self.lbl.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
self.btn = QPushButton("Preview")
self.btn.setMaximumSize(QtCore.QSize(100,100))
self.btn.clicked.connect(self.printsignal)
self.hbox = QHBoxLayout()
self.hbox.addWidget(self.lbl)
self.hbox.addWidget(self.btn)
self.setLayout(self.hbox)
def printsignal(self):
print("clicked", self.name)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__()
self.controls = QWidget()
self.controlsLayout = QVBoxLayout()
self.widgets = []
for name in tlist:
item = Label(name)
self.controlsLayout.addWidget(item)
self.widgets.append(item)
spacer = QSpacerItem(1, 1, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.controlsLayout.addItem(spacer)
self.controls.setLayout(self.controlsLayout)
self.scroll = QScrollArea()
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(self.controls)
self.searchbar = QLineEdit()
self.searchbar.textChanged.connect(self.update_display)
self.completer = QCompleter(tlist)
self.completer.setCaseSensitivity(Qt.CaseInsensitive)
self.searchbar.setCompleter(self.completer)
container = QWidget()
containerLayout = QVBoxLayout()
containerLayout.addWidget(self.searchbar)
containerLayout.addWidget(self.scroll)
container.setLayout(containerLayout)
self.setCentralWidget(container)
self.setGeometry(600, 100, 800, 600)
self.setWindowTitle('Search Engine')
def update_display(self, text):
for widget in self.widgets:
if text.lower() in widget.name.lower():
widget.show()
else:
widget.hide()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I have a Python app that uses PyQt5 for it's GUI. I have a Tab Widget in it, and I want to add and remove tabs outside of window class. Something like:
Tabs.addTab("name")
How do I do that?
Here is my code:
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTabWidget ,QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Test'
self.left = 0
self.top = 0
self.width = 500
self.height = 500
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.table_widget = MyTableWidget(self)
self.setCentralWidget(self.table_widget)
self.show()
class MyTableWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tabs.resize(300,200)
self.tabs.addTab(self.tab1, "Tab 1")
self.tabs.addTab(self.tab2, "Tab 2")
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Thank you for your help!
It does not matter if you are going to remove the tab within the class or outside of it but you have to use the QTabWidget object, for example in your case if you want to add a tab from the "App" class then you must do it through the object "table_widget" whose attribute is "tabs" which is the QTabWidget:
class App(QMainWindow):
def __init__(self):
super().__init__()
# ...
self.table_widget.tabs.addTab(QWidget(), "name") # <--- add tab
self.table_widget.tabs.removeTab(0) # <--- remove tab
I am just started to use the PyQt5 and I am trying to update the information of a label, through a button that is inside a subclass (QDialog). When I push the button, the program stop and show the message:
"AttributeError: 'New_Player_Window' object has no attribute 'name_window_label'
The idea is that when the Button is pressed, the Qlabel that by default is "Anonimous" become the Name of the User.
The code is:
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout,
QLineEdit, QLabel, QWidget, QPushButton, QMessageBox, QAction, QMenu,
QDialog
import sys
class General_Window(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.resize(500, 500)
self.move(300, 100)
self.setWindowTitle('Black Jack')
#MENUBAR
menubar = self.menuBar()
fileMenu = menubar.addMenu('File')
newAct = QAction('New Player', self)
newAct.triggered.connect(General_Window.new_player)
fileMenu.addAction(newAct)
#LABEL
self.name_window_label = QLabel('Anonimous', self)
self.name_window_label.move(245, 15)
self.show()
def update_window(self, value):
print(str(value))
self.name_window_label.setText(str(value))
def new_player(self):
class New_Player_Window(QDialog, General_Window):
def __init__(self):
super().__init__()
self.initUI()
def create(self):
try:
int(self.money.text()) + 2
except:
QMessageBox.question(self, 'PyQt5 message', "You need to
insert a Number", QMessageBox.Ok , QMessageBox.Ok)
else:
global value
value = self.name.text()
print(value)
self.update_window(value)
def initUI(self):
self.setGeometry(300, 230, 250, 120)
self.setWindowTitle('User Information')
#TEXTBOX1
self.name = QLineEdit(self)
self.name.move(110, 5)
self.name.resize(110,20)
#TEXTBOX2
self.money = QLineEdit(self)
self.money.move(110, 40)
self.money.resize(110,20)
#BUTTON1
self.button = QPushButton('Create', self)
self.button.move(5,80)
self.button.clicked.connect(self.create)
#BUTTON2
self.button2 = QPushButton('Cancel', self)
self.button2.move(120,80)
#LABELNAME
self.name_label = QLabel('SHORT NAME', self)
self.name_label.move(20,10)
#LABELNAME
self.money_label = QLabel('MONEY AVAILABLE', self)
self.money_label.move(10,45)
self.show()
self.exec_()
if __name__=="__main__":
New_Player_Window()
if __name__=="__main__":
app = QApplication(sys.argv)
ag = General_Window()
sys.exit(app.exec_())
You have several errors:
As you point out, General_Window has the name_window_label attribute so it would be expected that New_Player_Window would have it too, but name_window_label is created in initUI but you have overwritten it in the New_Player_Window class so that does not exist that attribute but even if it had it, it would not be the name_window_label of the other window since it is another class that has another object, I recommend reading about OOP and especially about inheritance and composition.
Having an internal class of another class is considered a bad practice (with exceptions such as django) since you are creating the class at every moment spending resources unnecessarily.
This is not an error in itself but is a bad practice, do not use global variables since debugging a global variable is complicated since it has a difficult life cycle that can hide other types of problems.
Finally consider using the appropriate widgets for the user to enter the appropriate data type, for example use QSpinBox for integer values so you avoid unnecessary checking. I also add the recommendation to the use of layouts as I will show in my answer.
Going to the design of the solution, when you create a widget consider it as a black box that receives inputs and generates outputs, if the output is synchronous it uses a method where you can retrieve that information and if it is asynchronous it uses a signal, on the other QDialog side is a class specialized in requesting information so you should not update the information in New_Player_Window but in General_Window for it you must pass the information. QDialog uses exec_() to return if the user accepts or rejects the request but for this you must call accept or reject.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class New_Player_Window(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.initUI()
def get_values(self):
return self.name.text(), self.money.value()
def initUI(self):
self.setWindowTitle('User Information')
#TEXTBOX1
self.name = QtWidgets.QLineEdit()
#TEXTBOX2
self.money = QtWidgets.QSpinBox(maximum=2147483647)
#BUTTON1
self.button = QtWidgets.QPushButton('Create')
self.button.clicked.connect(self.accept)
#BUTTON2
self.button2 = QtWidgets.QPushButton('Cancel')
self.button2.clicked.connect(self.reject)
lay = QtWidgets.QVBoxLayout(self)
flay = QtWidgets.QFormLayout()
flay.addRow("SHORT NAME", self.name)
flay.addRow("MONEY AVAILABLE", self.money)
lay.addLayout(flay)
hlay = QtWidgets.QHBoxLayout()
hlay.addWidget(self.button)
hlay.addWidget(self.button2)
lay.addLayout(hlay)
self.setFixedSize(self.sizeHint())
class General_Window(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Black Jack')
#MENUBAR
menubar = self.menuBar()
fileMenu = menubar.addMenu('File')
newAct = QtWidgets.QAction('New Player', self)
newAct.triggered.connect(self.new_player)
fileMenu.addAction(newAct)
#LABEL
self.name_window_label = QtWidgets.QLabel('Anonimous', alignment=QtCore.Qt.AlignCenter)
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
lay = QtWidgets.QVBoxLayout(widget)
lay.addWidget(self.name_window_label, alignment=QtCore.Qt.AlignTop)
def update_window(self, value):
self.name_window_label.setText(value)
def new_player(self):
w = New_Player_Window()
if w.exec_() == QtWidgets.QDialog.Accepted:
name, value = w.get_values()
print(name, value)
self.update_window(name)
if __name__=="__main__":
app = QtWidgets.QApplication(sys.argv)
ag = General_Window()
ag.show()
sys.exit(app.exec_())
Try it:
import sys
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout,
QLineEdit, QLabel, QWidget, QPushButton,
QMessageBox, QAction, QMenu, QDialog, QSpinBox)
class General_Window(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.resize(500, 500)
self.move(300, 100)
self.setWindowTitle('Black Jack')
#MENUBAR
menubar = self.menuBar()
fileMenu = menubar.addMenu('File')
newAct = QAction('New Player', self)
newAct.triggered.connect(self.new_player)
fileMenu.addAction(newAct)
#LABEL
self.name_window_label = QLabel('Anonimous', self)
self.name_window_label.move(245, 15)
def update_window(self, value):
print(str(value))
self.name_window_label.setText(str(value))
def new_player(self):
self.newPlayerWindow = New_Player_Window(self)
class New_Player_Window(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent
self.value = 12
self.initUI()
def create(self):
value = "{} - {}".format(self.name.text(), self.value)
print(value)
self.parent.update_window(value)
self.close()
def initUI(self):
self.setGeometry(300, 230, 250, 120)
self.setWindowTitle('User Information')
#TEXTBOX1
self.name = QLineEdit(self)
self.name.move(110, 5)
self.name.resize(110,20)
#TEXTBOX2
self.money = QSpinBox(self) #QLineEdit(self)
self.money.setRange(1, 99)
self.money.setValue(12)
self.money.valueChanged.connect(self.moneyValueChanged)
self.money.move(110, 40)
self.money.resize(110,20)
#BUTTON1
self.button = QPushButton('Create', self)
self.button.move(5,80)
self.button.clicked.connect(self.create)
#BUTTON2
self.button2 = QPushButton('Cancel', self)
self.button2.move(120,80)
#LABELNAME
self.name_label = QLabel('SHORT NAME', self)
self.name_label.move(20,10)
#LABELNAME
self.money_label = QLabel('MONEY AVAILABLE', self)
self.money_label.move(10,45)
self.exec_()
def moneyValueChanged(self, value):
self.value = value
if __name__=="__main__":
app = QApplication(sys.argv)
ag = General_Window()
ag.show()
sys.exit(app.exec_())
from PyQt5.QtWidgets import QMainWindow, QApplication,QLineEdit, QPushButton, QWidget, QAction, QTabWidget,QVBoxLayout
from PyQt5.QtCore import (QCoreApplication, QObject, QRunnable, QThread,
QThreadPool, pyqtSignal)
import sys
import os
from shutil import copy2
import _thread
import time
class AThread(QThread):
def run(self):
count = 0
while count < 5:
time.sleep(1)
print("A Increasing")
count += 1
class Example(QWidget):
def __init__(self):
super().__init__()
self.setAcceptDrops(True)
self.setWindowTitle('Learn')
self.setGeometry(300, 300, 300, 150)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tabs.resize(300,200)
# Add tabs
self.tabs.addTab(self.tab1,"Tab 1")
self.tabs.addTab(self.tab2,"Tab 2")
# Create first tab
self.tab1.layout = QVBoxLayout(self)
self.pushButton1 = QPushButton("PyQt5 button")
self.pushButton1.clicked.connect(self.ON_PRESS)
self.textbox = QLineEdit(self)
self.tab1.layout.addWidget(self.textbox )
self.tab1.layout.addWidget(self.pushButton1)
self.tab1.setLayout(self.tab1.layout)
#Create Textbox inputs
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
def using_q_thread(self):
app = Example()
thread = AThread()
thread.start()
sys.exit(app.exec_())
def ON_PRESS(self):
###Here is the Issue
try:
self.using_q_thread()
except:
print ("Error: unable to start thread")
###Drag and Drop files to directory
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
Hoping I am asking this correctly, but whenever using QThread there appears to be a bit of a hiccup. the first attempt to access the threaded function causes the try statement to fail, but then it immediately works. Im just curious if this is part of the functionality or if there is any issue with my code.
Avoid using try-except as you see hidden the error, in my personal case I avoid using it as far as I can for this type of problems.
I do not see it necessary to create another Example within using_q_thread, another problem is that thread is a local variable that will be eliminated, so thread must be a member of the class for its scope to increase.
import sys
import time
from PyQt5.QtCore import QThread
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTabWidget, QPushButton, QLineEdit
class AThread(QThread):
def run(self):
count = 0
while count < 5:
time.sleep(1)
print("A Increasing")
count += 1
class Example(QWidget):
def __init__(self):
super().__init__()
self.setAcceptDrops(True)
self.setWindowTitle('Learn')
self.setGeometry(300, 300, 300, 150)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tabs.resize(300,200)
# Add tabs
self.tabs.addTab(self.tab1,"Tab 1")
self.tabs.addTab(self.tab2,"Tab 2")
# Create first tab
self.tab1.layout = QVBoxLayout()
self.pushButton1 = QPushButton("PyQt5 button")
self.pushButton1.clicked.connect(self.ON_PRESS)
self.textbox = QLineEdit(self)
self.tab1.layout.addWidget(self.textbox )
self.tab1.layout.addWidget(self.pushButton1)
self.tab1.setLayout(self.tab1.layout)
#Create Textbox inputs
# Add tabs to widget
self.layout.addWidget(self.tabs)
def using_q_thread(self):
self.thread = AThread()
self.thread.start()
def ON_PRESS(self):
self.using_q_thread()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())