Illustration of Problem
I decided to work with PyQt and immediately encountered a problem and that is the text from my buttons is not displayed correctly and sometimes the buttons are not displayed completely.
import sys
from PyQt5 import QtWidgets
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
button = QtWidgets.QCheckBox(window)
button.setText("Hello World")
button.show()
window.show()
sys.exit(app.exec_())
Related
This seems like a bug, but I wanted to confirm before reporting it.
I am trying to update a label's text on a transparent widget, but for some reason the previous text is partially visible (see screenshot below)
Did any one face this issue before? Is there any known workarounds?
System Specs: MacOS Monterey 12.0.1 and Python 3.10
screenshot
from PyQt6 import QtWidgets, QtCore
from PyQt6.QtCore import QTimer
from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QLabel
def update_label():
l1.setText("Bye!")
window.repaint()
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setAttribute(QtCore.Qt.WidgetAttribute.WA_TranslucentBackground)
window.setWindowFlag(QtCore.Qt.WindowType.FramelessWindowHint)
window.setFixedSize(800, 600)
font = QFont()
font.setPointSize(72)
l1 = QLabel(window)
l1.setText("Hello World")
l1.setFont(font)
l1.setStyleSheet("color:red")
window.show()
timer = QTimer()
timer.setInterval(10000)
timer.timeout.connect(update_label)
timer.start()
app.exec()
Not sure it could be considered an answer, but I found a workaround.
Simply close then show the window again:
def update_label():
window.close()
window.show()
l1.setText('Bye!')
I created a GUI where if you click to a button than it will open a python file. But after the click I want to just see the console. How can I hide the GUI or close without closing the console part?
(Windows 10, Python 3.8.5)
You can hide it.
from PyQt5 import QtWidgets
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = QtWidgets.QWidget()
win.show()
win.setVisible(False)
sys.exit(app.exec_())
I'm newbie.
I want to click a pushButton to open a new window and take text from main window lineEdit and copy to new pop-up window lineEdit.
So far I an create new window but can't access lineEdit. No errors, app is not responding.
This is what I have:
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic
app = QApplication([]) #Main Window
ui = uic.loadUi(r"D:\UI_test\gui\main_gui_TT.ui")
appedit = QApplication([]) #Pop-up
uiedit = uic.loadUi(r"D:\UI_test\gui\input_TT.ui")
def edit1():
uiedit.show()
appedit.exec_()
uiedit.lineEdit_CC.setText('text') <-this line is a problem
ui.pushButton_1edit.pressed.connect(edit1)
ui.show()
app.exec_()
Please help what is wrong here?
You should only have a single QApplication even if you have many windows, considering the above the solution is:
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic
app = QApplication([]) # Main Window
ui = uic.loadUi(r"D:\UI_test\gui\main_gui_TT.ui")
uiedit = uic.loadUi(r"D:\UI_test\gui\input_TT.ui")
def edit1():
uiedit.show()
uiedit.lineEdit_CC.setText("text")
ui.pushButton_1edit.pressed.connect(edit1)
ui.show()
app.exec_()
Hi recently I started experimenting with the PyQt5 GUI library since Tkinter doesn't look very modern or nice. I am trying to create a very simple window with a button on it, but for some reason it opens a new window for the button. How can I solve this? Here is the code:
from PyQt5 import QtWidgets
app = QtWidgets.QApplication([])
window = QtWidgets.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("UI")
window.show()
btn = QtWidgets.QPushButton("Test")
btn.resize(100,100)
btn.move(100,100)
btn.show()
(app.exec_())
I am aware that I should be using classes for the GUI, but I'd like to learn the basics before I start doing that.
For a widget like the QPushButton to be part of a window, the widget must be fulfilled:
be child of the window or
be a child of some child from the window or
it is part of a layout that belongs to the window.
In your case QPushButton does not meet any of it so it will be a new window.So it can be solved using the 1 or 3 rule:
import sys
from PyQt5 import QtWidgets
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("UI")
btn = QtWidgets.QPushButton("Test", window)
btn.resize(100,100)
btn.move(100,100)
window.show()
sys.exit(app.exec_())
Or:
import sys
from PyQt5 import QtWidgets
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("UI")
btn = QtWidgets.QPushButton("Test")
lay = QtWidgets.QHBoxLayout(window)
lay.addWidget(btn)
window.show()
sys.exit(app.exec_())
I have problem with QTDesigner 5, which should be trivial, but I just can't figure out the problem.
What I want to do is to open a second Window when clicking on a button:
I have designed the Main Window and the secondary one with QTDesigner (PyQT5!) and converted them with pyuic to .py files. The Main Window opens without problems with the following Code:
from PyQt5 import QtGui, QtWidgets, QtCore, uic
import UI14 as UIImport
import GIPrompt as GIImport
class MainWindow(UIImport.Ui_MainWindow):
def __init__(self, window):
UIImport.Ui_MainWindow.__init__(self)
self.setupUi(window)
self.radioButtonGI.clicked.connect(self.openGIPrompt)
def openGIPrompt(self):
windowGI = QtWidgets.QDialog()
Gi = GIPrompt(windowGI)
windowGI.show()
class GIPrompt(GIImport.Ui_GIPrompt):
def __init__(self, windowGI):
GIImport.Ui_GIPrompt.__init__(self)
self.setupUi(windowGI)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)
window.show()
sys.exit(app.exec_())
If I add the following to the main function, the "GiPrompt" Window opens as well along with the Main Window:
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)
window.show()
"""Open secondWindow"""
windowGI = QtWidgets.QDialog()
Gi = GIPrompt(windowGI)
windowGI.show()
sys.exit(app.exec_())
If I try to open the second window via the openGIPrompt function, nothing happens. I do not get an error message, and no window appears. A print command however tells me that the init_function of the second Window is called...
Has someone an idea, what the problem could be?
Thanks in advance!
I have figured out the problem:
Apparently, the initialized Window is disposed of by garbage collection, as the variables are not declared as self:
This fixed the problem:
from PyQt5 import QtGui, QtWidgets, QtCore, uic
import UI14 as UIImport
import GIPrompt as GIImport
class MainWindow(UIImport.Ui_MainWindow):
windowGI=None
Gi=None
def __init__(self, window):
UIImport.Ui_MainWindow.__init__(self)
self.setupUi(window)
self.radioButtonGI.clicked.connect(self.openGIPrompt)
def openGIPrompt(self):
self.windowGI = QtWidgets.QDialog()
self.Gi = GIPrompt(self.windowGI)
self.windowGI.show()
class GIPrompt(GIImport.Ui_GIPrompt):
def __init__(self, windowGI):
GIImport.Ui_GIPrompt.__init__(self)
self.setupUi(windowGI)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)
window.show()
sys.exit(app.exec_())