I started reading the zetcode tutorial for PyQt4 (http://zetcode.com/tutorials/pyqt4/firstprograms/) and I was doing the tooltip part and all I did was copy and paste this bit of code. When I went to run it, the push button did not display in the window. Any reason as to why this might be? new to PyQt4 and Qt in general.
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')
btn = QtGui.QPushButton('Button, self')
btn.setToolTip('This is a <b>QPushButton</b> widget')
btn.resize(btn.sizeHint())
btn.move(50, 50)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Tooltips')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Replace following line:
btn = QtGui.QPushButton('Button, self')
with:
btn = QtGui.QPushButton('Button', self)
btn = QtGui.QPushButton('Button, self')
supposed to be:
btn = QtGui.QPushButton('Button', self)
Related
Here is my Code. I want to capture a pressed key and print it. But Nothing will Happen, How to resolve it? I Have two classes. One is Create_Instance and another one is Main_Window. In My code, I have Only Lable items to display. How to activate it?
Edited
import sys
from PyQt5.QtWidgets import *
class Create_Instance(QWidget):
def __init__(self,dict_item):
super().__init__()
self.dict_items = dict_item
self.lbl = QLabel("This is My Label")
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.lbl)
self.setLayout(self.vbox)
def keyPressEvent(self, event):
print(event)
print(self.dict_items)
class Main_Window(QWidget):
def __init__(self):
super(). __init__()
self.setWindowTitle("Main Window")
self.layout_main = QHBoxLayout()
self.firstmenu_container = Create_Instance(dict_item="1")
self.secondmenu_container = Create_Instance(dict_item="2")
self.layout_main.addWidget(self.firstmenu_container)
self.layout_main.addWidget(self.secondmenu_container)
self.setLayout(self.layout_main)
def main():
app = QApplication(sys.argv)
ex = Main_Window()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You must add the widget to the layout:
def __init__(self):
super().__init__()
self.setWindowTitle("Main Window")
self.layout_main = QHBoxLayout()
self.firstmenu_container = Create_Instance(dict_item="1")
self.layout_main.addWidget(self.firstmenu_container)
self.secondmenu_container = Create_Instance(dict_item="2")
self.layout_main.addWidget(self.secondmenu_container)
self.setLayout(self.layout_main)
I know the 2nd line I commented out doesn't work, just a representation of what I was thinking. This would be running the whole time the program is, so it could adjust to size changes.
Is something like this possible?
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 500, 300)
self.setWindowTitle("SciCalc")
self.setWindowIcon(QtGui.QIcon('atom.png'))
# self.setFixedSize(1000,800)
self.home()
def home(self):
btn = QtGui.QPushButton("Physics", self)
btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
btn.resize(100, 100)
btn.resize(100, 100)
# btn.move(width/2,height/2)
self.show()
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
Assuming that what you want is that the button stays in the middle of the window always, that you could do it by overwriting the resizeEvent method.
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 500, 300)
self.setWindowTitle("SciCalc")
self.setWindowIcon(QtGui.QIcon('atom.png'))
self.home()
def home(self):
self.btn = QtGui.QPushButton("Physics", self)
self.btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
self.btn.resize(100, 100)
self.show()
def resizeEvent(self, event):
self.btn.move(self.rect().center()-self.btn.rect().center())
QtGui.QMainWindow.resizeEvent(self, event)
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
I want to make a specific button in PyQt toolbar appeared as pressed(with blue background). Suppose when I hit the toolbar button I want it to be appeared as pressed
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 700, 700)
self.setWindowTitle('Rich Text Editor')
self.statusBar = QStatusBar()
self.textEdit = QtGui.QTextEdit()
self.setCentralWidget(self.textEdit)
self.setStatusBar(self.statusBar)
self.home()
def home(self):
changeBoldActionTB = \
QtGui.QAction(QtGui.QIcon('bold-text-option.png'),
'Make the text bold', self)
changeBoldActionTB.triggered.connect(self.changeBold)
self.formatbar = QToolBar()
self.addToolBar(Qt.TopToolBarArea, self.formatbar)
self.formatbar.addAction(changeBoldActionTB)
self.show()
def changeBold(self):
pass
#I think this does't matter
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
I have two toolbars.I am planning to use cursorPositionChanged to do this but still is there a way in PyQt to do this
reproduible code:
https://files.fm/u/h4c2amdx
Instead of using QAction you must use QToolButton and set the checkable property to True:
toolButton.setCheckable(True)
Example:
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setWindowTitle('Rich Text Editor')
self.statusBar = QStatusBar(self)
self.textEdit = QtGui.QTextEdit(self)
self.setCentralWidget(self.textEdit)
self.setStatusBar(self.statusBar)
self.home()
def home(self):
toolButton = QToolButton(self)
toolButton.setIcon(QtGui.QIcon('bold-text-option.png'))
toolButton.setCheckable(True)
toolButton.toggled.connect(self.onToggled)
self.formatbar = QToolBar(self)
self.addToolBar(Qt.TopToolBarArea, self.formatbar)
self.formatbar.addWidget(toolButton)
def onToggled(self, checked):
print(checked)
Screenshots:
Plus: To set the value manually and to obtain the status the following instructions are used:
toolButton.setChecked(True) # set State
print(toolButton.isChecked()) # get State
toolButton.toggle() # change state
Actions also have a checable flag and they can act as a toggle switch. Sorry, I don’t know English. So I showed it with code.
# add this code before self.formatbar = QToolBar()
# and create img file blue_bold-text-option.png
self.changeBoldActionTB.setCheckable(True)
icon = QIcon()
icon.addFile('bold-text-option.png', QSize(), QIcon.Normal, QIcon.Off)
icon.addFile('blue_bold-text-option.png', QSize(), QIcon.Normal, QIcon.On)
self.changeBoldActionTB.setIcon(icon)
I was trying to set a shortcut for dropdown menu. The code is below
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QAction
class SystemTrayIcon(QtWidgets.QSystemTrayIcon):
def __init__(self, icon, parent=None):
super(SystemTrayIcon, self).__init__(icon, parent)
menu = QtWidgets.QMenu(parent)
# add actions and shortcuts for them.
exitAction = QAction('Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(parent.close)
menu.addAction(exitAction)
restoreAction = QAction('Restore', self)
restoreAction.setShortcut("Ctrl+R")
restoreAction.triggered.connect(self.restore)
menu.addAction(restoreAction)
self.setContextMenu(menu)
def restore(self):
print 'restore'
class MainWindow(QtWidgets.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Icon')
self.tray_icon = SystemTrayIcon(QtGui.QIcon('/opt/WizNote/wiznote.png'), self)
self.tray_icon.show()
self.show()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
w = MainWindow()
sys.exit(app.exec_())
The shortcut doesn't work. Is there something wrong with my code?
Python2.7 and PyQt5 was installed in my laptop.
I had exactly the same problem as yours, and I solved it by respecting the following pattern for each action:
menu.addAction(exitAction)
parent.addAction(exitAction) # line to be added
I want to open/run a *.py file with pythonw.exe, when the Start-Button is clicked. Can anyone tell me how this works? I havent found the right function anywhere.
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')
btn1 = QtGui.QPushButton('Start', self)
# OPENFILE SOMEHOW!!
btn1.resize(btn1.sizeHint())
btn1.move(20, 20)
qbtn = QtGui.QPushButton('Quit', self)
qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(150, 20)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Python Script')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You can use subprocess.call. For example, this code runs external.py , when the Start is clicked:
import sys
from PyQt4 import QtGui, QtCore
import subprocess
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def run(self, path):
subprocess.call(['pythonw',path])
def initUI(self):
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')
btn1 = QtGui.QPushButton('Start', self)
btn1.resize(btn1.sizeHint())
btn1.move(20, 20)
btn1.clicked.connect(lambda:self.run('external.py'))
qbtn = QtGui.QPushButton('Quit', self)
qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(150, 20)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Python Script')
#subprocess.call(['pythonw','3.py'])
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()