Change color in QLineEdit in Python - python

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_())

Related

Problem integrating QFontComboBox in Qmenu

I'm trying to integrate QFontComboBox within Qmenu.
I try to make two things happen when selecting a particular font from the menu:
The Qmenu will close.
print the selected font.
from PyQt5.QtCore import QObject
from PyQt5.QtGui import QIcon, QFont, QCursor
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QFontComboBox, QWidgetAction, QMenu, QPushButton
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Pyside2 FontCombo Box")
self.setGeometry(300,200,300,250)
self.setFontBox()
self.setIcon()
self.show()
def setIcon(self):
appIcon = QIcon("icon.png")
self.setWindowIcon(appIcon)
def setFontBox(self):
self.font_button = QPushButton(self)
self.font_button.setFixedWidth(300)
self.font_button.clicked.connect(lambda: self.setFontmenu())
vbox = QVBoxLayout()
vbox.addWidget(self.font_button)
self.setLayout(vbox)
def setFontmenu(self):
font_menu = QMenu()
font_submenu = QFontComboBox()
font_submenu.setCurrentFont(QFont("Arial"))
objectTest = QObject()
widget = QWidgetAction(objectTest)
widget.setDefaultWidget(font_submenu)
font_menu.addAction(widget)
font_menu.exec_(QCursor.pos())
menu = font_menu
menu.addSeparator()
font_submenu.showPopup()
font_submenu.setFocus()
font_submenu.currentFontChanged.connect(self._changed)
def _changed(self):
font = self.currentFont().family()
print(font)
return
myapp = QApplication(sys.argv)
window = Window()
myapp.exec_()
sys.exit()

PyQt5 - Tabs having same objects

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.

Disable higlighting selected items in QListWidget pyqt5

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?

how to get the input on the textbox in pyqt using another py script

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())

QScrollArea does not attach to label

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)

Categories

Resources