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()
Related
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()
How can I make (on_click) works when QPushButton ("click") is pressed by Enter keyboard key? it only interacts with mouse_click
import sys
from PyQt5.QtWidgets import *
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.label = QLabel("",self)
self.label.move(100, 100)
self.button = QPushButton('click', self)
self.button.move(100, 50)
self.button.clicked.connect(self.on_click)
self.setGeometry(500, 150, 200, 200)
self.show()
def on_click(self):
self.label.setText("Hello")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
You have to overwrite the keyPressEvent method:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.label = QLabel("",self)
self.label.move(100, 100)
self.button = QPushButton('click', self)
self.button.move(100, 50)
self.button.clicked.connect(self.on_click)
self.setGeometry(500, 150, 200, 200)
self.show()
def on_click(self):
self.label.setText("Hello")
def keyPressEvent(self, event):
if event.key() == Qt.Key_Return:
self.on_click()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
I found a solution regarding this: keyPressEvent() method doesn't work for PyQt5 / Python 3+.
You need to override the function in super class.
MainWindow.keyPressEvent = self.keyPressEvent
I am having troubles to understand class inheritance with Python/PyQt. I have a MainWindow and a Popup QWidget. I want to interact with the self.label1 of the MainWindow after the QWidget was opened in a pop up window but I don't know how to do it. I know only the other way around, to reach all widgets from the popup Window inside MainWindow but not vice versa.
Here is an example, self.label1 of MainWindow should get another text after MyPopup opens in a new window:
import sys
from PyQt4.Qt import *
class MyPopup(QWidget):
def __init__(self):
QWidget.__init__(self)
# I want to change the lable1 of MainWindow
self.cw.label1.setText('hello')
class MainWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.cw = QWidget(self)
self.setCentralWidget(self.cw)
self.btn1 = QPushButton("Click me", self.cw)
self.btn1.setGeometry(QRect(50, 50, 100, 30))
self.label1 = QLabel("No Commands running", self.cw)
self.connect(self.btn1, SIGNAL("clicked()"), self.doit)
self.w = None
def doit(self):
self.w = MyPopup()
self.w.setGeometry(QRect(100, 100, 400, 200))
self.w.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
You need to pass the main window as a parameter to the constructor of MyPopup, try this:
import sys
from PyQt4.Qt import *
class MyPopup(QWidget):
def __init__(self, mainWindow):
QWidget.__init__(self)
# use the mainWindow passed as parameter
mainWindow.label1.setText('hello')
class MainWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.cw = QWidget(self)
self.setCentralWidget(self.cw)
self.btn1 = QPushButton("Click me", self.cw)
self.btn1.setGeometry(QRect(50, 50, 100, 30))
self.label1 = QLabel("No Commands running", self.cw)
self.connect(self.btn1, SIGNAL("clicked()"), self.doit)
self.w = None
def doit(self):
self.w = MyPopup(self) #when creating the popup pass in the main window
self.w.setGeometry(QRect(100, 100, 400, 200))
self.w.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
This is the prior answer from Alvaro Fuentes, with the minor updates necessary for PyQt5.
import sys
from PyQt5.Qt import *
class MyPopup(QWidget):
def __init__(self, mainwin):
QWidget.__init__(self)
# I want to change the lable1 of MainWindow
mainwin.label1.setText('hello')
class MainWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.cw = QWidget(self)
self.setCentralWidget(self.cw)
self.btn1 = QPushButton("Click me", self.cw)
self.btn1.setGeometry(QRect(50, 50, 100, 30))
self.label1 = QLabel("No Commands running", self.cw)
self.btn1.clicked.connect(self.doit)
self.w = None
def doit(self):
self.w = MyPopup(self)
self.w.setGeometry(QRect(100, 100, 400, 200))
self.w.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
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)
I just want to display system clock time in LCD format. I also want the time to be displayed using hh:mm:ss format. My code is below. But when I run it, it is not as I expected. Can anyone explain why?
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.showlcd()
def initUI(self):
self.lcd = QtGui.QLCDNumber(self)
self.setGeometry(30, 30, 800, 600)
self.setWindowTitle('Time')
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.lcd)
self.setLayout(vbox)
self.show()
def showlcd(self):
time = QtCore.QTime.currentTime()
text = time.toString('hh:mm:ss')
self.lcd.display(text)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
QLCDNumber has a fixed digit display (QLCDNumber.digitCount) and its default value is 5. So your text is truncated to the last 5 characters. You should set an appropriate value (8 in your case).
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.showlcd()
def initUI(self):
self.lcd = QtGui.QLCDNumber(self)
self.lcd.setDigitCount(8) # change the number of digits displayed
self.setGeometry(30, 30, 800, 600)
self.setWindowTitle('Time')
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.lcd)
self.setLayout(vbox)
self.show()
def showlcd(self):
time = QtCore.QTime.currentTime()
text = time.toString('hh:mm:ss')
self.lcd.display(text)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()