So I do not want to use a layout for any of the tabs. I will be using .setGeometry to place the objects, as there is many objects to be placed :)
The problem is: Any object created in Tab1.py and Tab2.py are appearing on all the tabs.
Tab3.py code is identical to Tab2.py (just a button)
I believe the issue is with the Parent/Child code, but not sure were I went wrong. Any guidance would be much appreciated. You can also ignore some of the imports, I just haven't gotten to that code yet :)
MainWindow.py
import os, sys, subprocess, atexit, PyQt5, pyodbc, time, datetime, getpass, csv, xlsxwriter
import Tab1, Tab2, Tab3
from PyQt5.QtWidgets import QApplication, QTabBar, QLayout, QLabel, QLineEdit, QMainWindow, QTabWidget, QWidget, QPushButton, QHBoxLayout, QVBoxLayout, QGroupBox, QRadioButton, QTextEdit, QCheckBox, QInputDialog, QFileDialog, QProgressBar, QTableWidget, QTableWidgetItem
from PyQt5.QtCore import *
class MainWindow(QMainWindow):
# define main window size and position on screen
def __init__(MainWindow, parent=None):
super().__init__()
MainWindow.left = 50
MainWindow.top = 50
MainWindow.width = 720
MainWindow.height = 600
MainWindow.setGeometry(MainWindow.left,
MainWindow.top,
MainWindow.width,
MainWindow.height)
MainWindow.table_widget = MainWindowWidget(MainWindow)
MainWindow.setCentralWidget(MainWindow.table_widget)
MainWindowWidget()
class MainWindowWidget(QWidget):
def __init__(self, parent=None):
super(QWidget, self).__init__(parent)
# configure layout
self.layout = QVBoxLayout(self)
self.setLayout(self.layout)
# initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.layout.addWidget(self.tabs)
# add tabs
self.tabs.addTab(self.tab1, "Tab1")
self.tabs.addTab(self.tab2, "Tab2")
self.tabs.addTab(self.tab2, "Tab3")
# create first tab
Tab1.Tab1.CreateTab1(self)
Tab2.Tab2.CreateTab2(self)
Tab3.Tab3.CreateTab3(self)
def exit_handler(self):
print('closing application')
atexit.register(exit_handler(self))
# create main window and show it
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = MainWindow()
MainWindow.show()
sys.exit(app.exec_())
Tab1.py
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QMainWindow, QTabWidget, QWidget, QPushButton, QHBoxLayout, \
QVBoxLayout, QGroupBox, QRadioButton, QTextEdit, QCheckBox, QInputDialog, QFileDialog, QProgressBar, QTableWidget, \
QTableWidgetItem, QComboBox
import MainWindow
class Tab1(QMainWindow):
def CreateTab1(tab1, parent=None):
# reference number input box
tab1.label_input_reference_number = QLabel(tab1)
tab1.label_input_reference_number.setText('Reference Number')
tab1.label_input_reference_number.setGeometry(25, 80, 115, 20)
tab1.input_reference_number = QLineEdit(tab1)
tab1.input_reference_number.setGeometry(170, 80, 215, 20)
# add CheckGroupButton
tab1.Button = QPushButton(tab1)
tab1.Button.setText("Button")
tab1.Button.setGeometry(385, 105, 100, 20)
Tab2.py
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QMainWindow, QTabWidget, QWidget, QPushButton, QHBoxLayout, QGroupBox, QRadioButton, QTextEdit, QCheckBox, QInputDialog, QFileDialog, QProgressBar, QTableWidget, QTableWidgetItem
class Tab2(QMainWindow):
def CreateTab2(tab2):
# add button
tab2.Button = QPushButton(tab2)
tab2.Button.setText("Another button")
tab2.Button.setGeometry(300, 585, 100, 20)
Your problem is here:
self.tabs.addTab(self.tab1, "Tab1")
self.tabs.addTab(self.tab2, "Tab2")
self.tabs.addTab(self.tab2, "Tab3")
# create first tab
Tab1.Tab1.CreateTab1(self)
Tab2.Tab2.CreateTab2(self)
Tab3.Tab3.CreateTab3(self)
You have all of the tab controls being owned by the parent. (Also, you have tab2 in there twice.) You want:
self.tabs.addTab(self.tab1, "Tab1")
self.tabs.addTab(self.tab2, "Tab2")
self.tabs.addTab(self.tab3, "Tab3")
Tab1.Tab1.CreateTab1(tab1)
Tab2.Tab2.CreateTab2(tab2)
Tab3.Tab3.CreateTab3(tab3)
However, this is an odd way to do it. You should have Tab1, Tab2 and Tab3 derive from QWidget, and do this:
self.tab1 = Tab1.Tab1()
self.tab2 = Tab2.Tab2()
self.tab3 = Tab3.Tab3()
That way, you don't need a strange class method to configure things.
Here's why: the tab{i} variable you're passing into each CreateTab{i}(tab{i}) function is the same variable each time; self as referenced in MainWindow. Here's what I'd do; instead of passing self into the call Tab{i}.Tab{i}.CreateTab{i}(self), instead pass in self.tab{i}, the variable you stored earlier. That way separate objects (not the same self i.e. MainWindow) are receiving the separate children widgets.
Related
The header of the next group overwrites the table of the one before. Any ideas on how to protect the table before? I tried to use the spacer but it did not work. Any other ideas how I can solve the problem?
I have a header with e.g. an account number and a name. Below I want to list all the postings. But this program is not only for one account. It is for 100+ accounts to document the balances at a give date. (e.g. at the end of the year)
from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog, QWidget,
QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox, QHBoxLayout,
QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox, QTextEdit,QScrollArea,
QVBoxLayout,QListWidget,QTableWidget,QTableWidgetItem)
from PyQt5.QtCore import Qt, pyqtSlot, pyqtSignal, QSize, QDate
import sys
class Dialog(QDialog):
def __init__(self,parent=None):
super().__init__(parent)
self.setGeometry(64,64, 1000, 400)
self.setWindowTitle("test")
self.scrollarea = QScrollArea(self)
self.scrollarea.setFixedWidth(990)
self.scrollarea.setWidgetResizable(True)
widget = QWidget()
self.scrollarea.setWidget(widget)
self.layout_SArea = QGridLayout(widget)
self.layout_All = QVBoxLayout(self)
self.layout_All.addWidget(self.scrollarea)
for i in range(1,20,2):
self.layout_SArea.addWidget(QLabel("I am the header"),i,0)
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(10)
self.tableWidget.setColumnCount(12)
for x in range(10):
newitem = QTableWidgetItem()
newitem.setData( Qt.EditRole,'{:0,.0f}'.format(x))
newitem.setFlags( Qt.ItemIsSelectable | Qt.ItemIsEnabled )
newitem.setTextAlignment(Qt.AlignRight)
self.tableWidget.setItem(x, 0, newitem)
self.layout_SArea.addWidget(self.tableWidget,i+1,0,i+1,5)
self.layout_All.addLayout(self.layout_SArea)
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = Dialog()
sys.exit(dialog.exec_())
The row span argument indicates how many "row cells" the in the grid are occupied by the item, so it should not use the row index.
Change to:
self.layout_SArea.addWidget(self.tableWidget, i+1, 0, 1, 5)
I changed the background color to gray and the label color to yellow, but the color of the boxes was also changed to gray and the text inside of them changed color to yellow.
I want to change the colors of the QLineEdit to white and the text inside of them to black, but I don't know how to do it
import sys, re
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QFormLayout, QCheckBox, QComboBox, QSpinBox, QDialogButtonBox, QMessageBox, QErrorMessage, QToolTip, QPushButton, QLineEdit, QLabel, QTextEdit, QMainWindow, QGroupBox, QHBoxLayout, QVBoxLayout
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt
import ctypes
class Dialog(QDialog):
NumGridRows = 10
NumButtons = 4
def __init__(self):
super(Dialog, self).__init__()
self.createFormGroupBox()
self.setStyleSheet("background:rgb(66,85,99);")
buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
buttonBox1 = QPushButton('Export',self)
buttonBox1.setStyleSheet("background:rgb(255,199,44);")
buttonBox.accepted.connect(self.accept)
buttonBox.rejected.connect(self.reject)
buttonBox.setStyleSheet("background:rgb(255,199,44);")
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.formGroupBox)
mainLayout.addWidget(buttonBox)
mainLayout.addWidget(buttonBox1)
self.setLayout(mainLayout)
self.setWindowTitle("Cuadro seguimiento")
def createFormGroupBox(self):
self.formGroupBox = QGroupBox("New")
self.formGroupBox.setStyleSheet("color: rgb(255,199,44);")
layout = QFormLayout()
layout.addRow(QLabel("Fecha"), QLineEdit())
layout.addRow(QLabel("Dias abiertos"), QLineEdit())
layout.addRow(QLabel("Nombre del caso"), QLineEdit())
layout.addRow(QLabel("Responsable"), QLineEdit())
layout.addRow(QLabel("Ok MKT"), QLineEdit())
layout.addRow(QLabel("Solicitud Lotus"), QCheckBox())
layout.addRow(QLabel("OS"), QLineEdit())
layout.addRow(QLabel("Monto"), QLineEdit())
layout.addRow(QLabel("Fecha cierre"), QLineEdit())
layout.addRow(QLabel("Comentario"), QLineEdit())
self.formGroupBox.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = Dialog()
sys.exit(dialog.exec_())
How do I disable higlighting selected items in QListWidget pyqt5?
Tried the following which didn't work:
from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QApplication, QListWidget, QListWidgetItem, QGridLayout, QLabel, QWidget
app = QApplication([])
widget = QWidget()
listWidget = QListWidget()
item = QListWidgetItem('Pogba', listWidget)
layout.addWidget(listWidget)
#Attempt
palette = QPalette()
palette.setColor(QPalette.Highlight, listWidget.palette().color(QPalette.Base))
palette.setColor(QPalette.HighlightedText, listWidget.palette().color(QPalette.Text))
listWidget.setPalette(palette)
widget.setLayout(layout)
widget.show()
app.exec()
And the following only makes the item grey instead of blue:
from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QApplication, QListWidget, QListWidgetItem, QGridLayout, QLabel, QWidget
app = QApplication([])
widget = QWidget()
listWidget = QListWidget()
item = QListWidgetItem('Pogba', listWidget)
layout.addWidget(listWidget)
#Other attempt
listWidget.setFocusPolicy(Qt.NoFocus)
widget.setLayout(layout)
widget.show()
app.exec()
Is there another way to achieve this?
I have these 2 scripts. First contains ui using pyqt5,this contains some textbox and buttons. The second one contains the functions of the buttons( i mean what it will do).For now I just want to display in the terminal on what is written in the textbox. The problem is i can't get/print/display the input text in the textbox using the second py. I have this button(upload button) which must display this text in the terminal when click.
I have tried to import the second py to first. Then i connect the button.
butUpld.clicked.connect(py2.up)
for the second py i also connect it to the first one
This is the first py file name py1.py
import os, glob, sys, time
import py2
from PyQt5.Qt import QApplication, QClipboard, QUrl
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QWidget, QPlainTextEdit, QLabel, QGridLayout, QPushButton, QLineEdit, QTextEdit, QMessageBox, QComboBox, QTabWidget, QFormLayout, QHBoxLayout, QRadioButton, QCheckBox, QVBoxLayout, QFileDialog, QButtonGroup
from PyQt5.QtCore import pyqtSlot, Qt
qt_app = QApplication(sys.argv)
class Layoutexe(QTabWidget):
def __init__(self, parent = None):
super(Layoutexe, self).__init__(parent)
self.tab1 = QWidget()
self.tab2 = QWidget()
self.addTab(self.tab1," ")
self.addTab(self.tab2," ")
self.tab1UI()
self.tab2UI()
self.setWindowTitle(" ")
self.setFixedSize(900,1000)
def tab1UI(self):
vbox = QVBoxLayout()
hbox = QHBoxLayout()
hbox1 = QHBoxLayout()
hbox2 = QHBoxLayout()
hbox3 = QHBoxLayout()
labelPath = QLabel("PATH:")
tboxPath = QLineEdit(self)
butSrch = QPushButton("Search", self)
butClr = QPushButton("Clear", self)
hbox.addStretch()
hbox.addWidget(labelPath)
hbox.addWidget(tboxPath)
hbox.addWidget(butSrch)
hbox.addStretch()
hbox.addWidget(butClr)
vbox.addLayout(hbox)
radbutLst = QRadioButton("Latest")
radbutMnl = QRadioButton("Manual")
hbox1.addWidget(radbutLst)
hbox1.addWidget(radbutMnl)
hbox1.addStretch()
vbox.addLayout(hbox1)
labelEV = QLabel("Event:")
tboxEV = QLineEdit(self)
hbox2.addWidget(labelEV)
hbox2.addWidget(tboxEV)
hbox2.addStretch()
vbox.addLayout(hbox2)
butUpld = QPushButton("UPLOAD", self)
hbox3.addStretch()
hbox3.addWidget(butUpld)
hbox3.addStretch()
vbox.addLayout(hbox3)
vbox.addStretch()
self.tab1.setLayout(vbox)
butUpld.clicked.connect(py2.up)
def tab2UI(self):
vbox = QVBoxLayout()
layout = QHBoxLayout()
def run(self):
self.show()
qt_app.exec_()
app = Layoutexe()
app.run()
this the second py file named py2.py
import os, glob
import py1
def up():
a = gui.rep.tab1U.tboxEV
print(a.text())
Try it:
py1.py
import sys
#import os, glob, time
from PyQt5.Qt import QApplication, QClipboard, QUrl
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QWidget, QPlainTextEdit, QLabel, QGridLayout, QPushButton, QLineEdit, QTextEdit, QMessageBox, QComboBox, QTabWidget, QFormLayout, QHBoxLayout, QRadioButton, QCheckBox, QVBoxLayout, QFileDialog, QButtonGroup
from PyQt5.QtCore import pyqtSlot, Qt
import py2
qt_app = QApplication(sys.argv)
class Layoutexe(QTabWidget):
def __init__(self, parent = None):
super(Layoutexe, self).__init__(parent)
self.tab1 = QWidget()
self.tab2 = QWidget()
self.addTab(self.tab1," ")
self.addTab(self.tab2," ")
self.tab1UI()
self.tab2UI()
self.setWindowTitle(" ")
self.setFixedSize(900,1000)
def tab1UI(self):
vbox = QVBoxLayout()
hbox = QHBoxLayout()
hbox1 = QHBoxLayout()
hbox2 = QHBoxLayout()
hbox3 = QHBoxLayout()
labelPath = QLabel("PATH:")
tboxPath = QLineEdit(self)
butSrch = QPushButton("Search", self)
butClr = QPushButton("Clear", self)
hbox.addStretch()
hbox.addWidget(labelPath)
hbox.addWidget(tboxPath)
hbox.addWidget(butSrch)
hbox.addStretch()
hbox.addWidget(butClr)
vbox.addLayout(hbox)
radbutLst = QRadioButton("Latest")
radbutMnl = QRadioButton("Manual")
hbox1.addWidget(radbutLst)
hbox1.addWidget(radbutMnl)
hbox1.addStretch()
vbox.addLayout(hbox1)
labelEV = QLabel("Event:")
self.tboxEV = QLineEdit(self)
hbox2.addWidget(labelEV)
hbox2.addWidget(self.tboxEV)
hbox2.addStretch()
vbox.addLayout(hbox2)
butUpld = QPushButton("UPLOAD", self)
hbox3.addStretch()
hbox3.addWidget(butUpld)
hbox3.addStretch()
vbox.addLayout(hbox3)
vbox.addStretch()
self.tab1.setLayout(vbox)
butUpld.clicked.connect(lambda _, s=self: py2.up(s)) # +++
def tab2UI(self):
vbox = QVBoxLayout()
layout = QHBoxLayout()
def run(self):
self.show()
qt_app.exec_()
app = Layoutexe()
app.run()
py2.py
#import os, glob
#import py1
def up(self):
# a = gui.rep.tab1U.tboxEV
a = self.tboxEV
print(a.text())
I am trying to display an image label with scrollbars within a Box layout.
However, the scroll area appears at the wrong place with the wrong size.
Could you please tell me what I am doing wrong?
import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton, QLabel, QScrollArea
from PyQt5.QtGui import QPixmap
class ApplicationWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
main_widget = QWidget(self)
btn = QPushButton("Bye", self)
btn.clicked.connect(self.close)
img = QPixmap("1.jpg")
label = QLabel(main_widget)
label.setPixmap(img)
scrollArea = QScrollArea(main_widget)
scrollArea.setWidgetResizable(True)
scrollArea.setWidget(label)
l = QVBoxLayout(main_widget)
l.addWidget(label)
l.addWidget(btn)
self.setCentralWidget(main_widget)
def closeEvent(self, ce):
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
aw = ApplicationWindow()
aw.show()
app.exec_()
The result is:
The problem is that instead of adding the QLabel to QVBoxLayout you must add the QScrollArea. You must change:
l.addWidget(label)
to
l.addWidget(scrollArea)