Python Forum
[PyQt] timer makes my qmessagebox crash
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] timer makes my qmessagebox crash
#11
To test it I've changed it. I have no PySide2
Reply
#12
(Feb-12-2020, 07:10 PM)Denni Wrote: Hey @Axel_Erfurt if you going to give an example why provide something that does not fully reflect proper design?
I mean it was a nice example and while not the ugliest I have seen it sure was not pretty either.

And if your defense is you were modifying the OP's code you should have gone the few extra minutes it would taken to clean it up ;)

Also @darktitan I would strongly suggest you stop using the Designer to create your GUI as you are only going to eventually experience a lot of pain with that methodology and frankly its just as easy and quick to design the GUI using proper Qt rather that what the Designer puts out since the Designer was not meant for that purpose.

Here is the previous example cleaned-up and made a bit more python-pyqt-ish

from PySide2.QtCore    import Qt, QTimer
from PySide2.QtWidgets import QApplication, QMainWindow, QWidget
from PySide2.QtWidgets import QVBoxLayout, QHBoxLayout
from PySide2.QtWidgets import QPushButton, QLabel, QMessageBox

from datetime import datetime as dtDateTime
from time     import sleep    as tmSleep

class CentralPanel(QWidget):
    def __init__(self) :
        QWidget.__init__(self)

        self.StartTime = 0
        self.ElpseTime = 0

        self.btnStart = QPushButton()
        self.btnStart.setText('Start Timer')
        self.btnStart.clicked.connect(self.StartTimer)

        self.btnStop = QPushButton()
        self.btnStop.setText('Stop Timer')
        self.btnStop.clicked.connect(self.StopTimer)
        
        HBox1 = QHBoxLayout()
        HBox1.addStretch(1)
        HBox1.addWidget(self.btnStart)
        HBox1.addStretch(1)
        HBox1.addWidget(self.btnStop)
        HBox1.addStretch(1)

        self.lblTimeOut = QLabel('Timer Display')
        self.lblTimeOut.setAlignment(Qt.AlignCenter)

        self.btnInfo = QPushButton()
        self.btnInfo.setText('Info')
        self.btnInfo.clicked.connect(self.ShowMsg)

        HBox2 = QHBoxLayout()
        HBox2.addStretch(1)
        HBox2.addWidget(self.btnInfo)
        HBox2.addStretch(1)
        
        VBox = QVBoxLayout()
        VBox.addWidget(self.lblTimeOut)
        VBox.addLayout(HBox1)
        VBox.addLayout(HBox2)
        VBox.addStretch(1)

        self.setLayout(VBox)

        self.btnStart.setFocus()

        self.timer = QTimer()
        self.timer.timeout.connect(self.UpdateLabel)

    def StartTimer(self):
        self.StartTime = dtDateTime.now()

        self.lblTimeOut.setText('Starting Timer ...')
        self.timer.start(1000)  #  1 second

    def StopTimer(self):
        if self.ElpseTime == 0:
            self.ElpseTime = dtDateTime.now() - self.StartTime
        else:
            self.ElpseTime = self.ElpseTime + (dtDateTime.now() - self.StartTime)

        self.timer.stop()
        self.lblTimeOut.setText('Timer Stopped')
        tmSleep(2)
        self.lblTimeOut.setText('Timer Display')

    def UpdateLabel(self):
        CurTime = dtDateTime.now()
        DsplyTime = 'Time: ' + CurTime.strftime('%H:%M:%S')
        self.lblTimeOut.setText(DsplyTime)

    def ShowMsg(self):
        Msg = QMessageBox()
        Msg.setIcon(QMessageBox.Information)
        Msg.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.SplashScreen)
        Msg.setText('Showing Current Elasped Time')
        Msg.setInformativeText('This is the total amount of elapsed run time')
        Msg.setDetailedText('The Elapsed Time Is: ' + str(self.ElpseTime))
        Msg.setStandardButtons(QMessageBox.Ok)
        Msg.setDefaultButton(QMessageBox.Ok)
     
        Msg.exec_()
 
class MyWindow(QMainWindow):
    def __init__(self) :
        QMainWindow.__init__(self)

        self.setWindowTitle('Time Tracker')
        self.resize(210, 120)
        CenterPane = CentralPanel()
        self.setCentralWidget(CenterPane)
    
if __name__ == '__main__':
    MainEvntHndlr = QApplication([])

    MainApp = MyWindow()
    MainApp.show()

    MainEvntHndlr.exec()

I usually use tkinter but this time i was a bit lazy an thought why not try out pyside2 and the designer. Big Grin And i like the ide of having the gui layout separate from the apps function code.tnx for the example.By the way the example its not how i wanted it to work. My code need the user to type in the time when the message will be shown hour minutes and seconds .
Reply
#13
Oh sorry I did not realize that -- I was redesigning the solution Axel_Erfurt had put forth just to create a more python-qt-ish example. As for having your GUI design being separate from the App that is perfectly fine -- just when you subclass QMainWindow put it within its own separate file -- easy peasy

@Axel_Erfurt I do not have PySide2 either I do all my coding in PyQt5 then simply replace the reference to PyQt5 with PySide2 this works about 95% of the time -- the only discrepancy I have come across is PyQt5 uses pyqtSignal and pyqtSlot while PySide2 references these as just Signal and Slot.
Reply
#14
(Feb-14-2020, 04:49 PM)Denni Wrote: Oh sorry I did not realize that -- I was redesigning the solution Axel_Erfurt had put forth just to create a more python-qt-ish example. As for having your GUI design being separate from the App that is perfectly fine -- just when you subclass QMainWindow put it within its own separate file -- easy peasy

@Axel_Erfurt I do not have PySide2 either I do all my coding in PyQt5 then simply replace the reference to PyQt5 with PySide2 this works about 95% of the time -- the only discrepancy I have come across is PyQt5 uses pyqtSignal and pyqtSlot while PySide2 references these as just Signal and Slot.

Here is something i put together in a hurry in tkinter yesterday it works like my pyside2 app.

import sys
from datetime import datetime
from threading import Timer
import tkinter as tk
from tkinter import messagebox


class HomePage(tk.Tk):
    def __init__(self):
        super().__init__()  


        self.geometry("200x140+10+10") 


        self.frame = tk.Frame(self) 
        self.frame.master.title("Tkinter") 
        self.frame.grid(padx=13, pady=13) 

        self.createWidgets() 

    def createWidgets(self):

        self.a1 = tk.IntVar()
        self.a2 = tk.IntVar()
        self.a3 = tk.IntVar()
        self.textmsg = tk.StringVar()
        

        self.myLabelH = tk.Label(self.frame, text="Hour")
        Hour = tk.Entry(self.frame, font=('Helvetica', 12, 'bold'), textvariable=self.a1, width = 5)
        self.myLabel2 = tk.Label(self.frame, text=":")
        self.myLabelM = tk.Label(self.frame, text="Minutes")
        Minutes = tk.Entry(self.frame, font=('Helvetica', 12, 'bold'), textvariable=self.a2, width = 5)
        self.myLabel3 = tk.Label(self.frame, text=":")
        self.myLabelS = tk.Label(self.frame, text="Seconds")
        Seconds = tk.Entry(self.frame, font=('Helvetica', 12, 'bold'), textvariable=self.a3, width = 5)
        self.myLabelmessage = tk.Label(self.frame, text="Text message")
        textmessage = tk.Entry(self.frame, font=('Helvetica', 12, 'bold'), textvariable=self.textmsg, width = 19)
        button1 = tk.Button(self.frame, text="Make",  command=self.ok_handler, width = 24)
       
        

        self.myLabelH.grid(row=1, column=0)
        self.myLabelM.grid(row=1, column=2)
        self.myLabelS.grid(row=1, column=4)
        self.myLabel2.grid(row=2, column=1)
        self.myLabel3.grid(row=2, column=3)
        Hour.grid(column=0, row=2, sticky=(tk.W))
        Minutes.grid(column=2, row=2, sticky=(tk.W))
        Seconds.grid(column=4, row=2, sticky=(tk.W))
        self.myLabelmessage.grid(row=3, column=1, columnspan=3, sticky=(tk.W))
        textmessage.grid(column=0, row=4, sticky=(tk.W), columnspan=5)
        button1.grid(column=0, row=5, sticky=(tk.W), columnspan=5, pady = 5)

    def ok_handler(self):
         
        x=datetime.today()
        y=x.replace(day=x.day+1, hour=(self.a1.get()), minute=(self.a2.get()), second=(self.a3.get()), microsecond=0)
        delta_t=y-x
 
        secs=delta_t.seconds+1
 
        t = Timer(secs, self.hello_world)
        t.daemon = True
        t.start()
         
    def hello_world(self):
        value1 = (self.textmsg.get())   
        print (value1)
         
        messagebox.showinfo("Title", value1)
           
def main():
    app = HomePage()
    app.mainloop()

if __name__ == '__main__':
    main()
Reply
#15
Okay so do you want that re-written in PySide2 or is that just an FYI?
Reply
#16
(Feb-18-2020, 03:32 PM)Denni Wrote: Okay so do you want the re-written in PySide2 or is that just an FYI?

Yea if you want to sure why not then i can compare and learn pyqt.
Reply
#17
Okay not exactly what you created but it should give you an idea of the how to -- also as I state in code you could have used a QGridLayout but I figured I would show the more free-form method as folks seem to have more trouble with that than using the more static Grid version. Also I did not implement the Message Box as there are plenty of examples of how to do that out there and I was not sure what yours was supposed to be doing. I hope this helps you with understanding PySide2 (or PyQt for that mattter as I used PyQt to make this then just referenced it to PySide2)
from PySide2.QtCore    import Slot
from PySide2.QtGui     import QFont
from PySide2.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout
from PySide2.QtWidgets import QPushButton

from datetime import datetime as dtDateTime

class DoubleLabel(QWidget):
    def __init__(self, Text, Value=''):
        QWidget.__init__(self)

        Font = QFont()
        Font.setFamily('Helvetica')
        Font.setPointSize(12)
        Font.setBold(True)

        self.lblText = QLabel(Text)
        self.lblText.setFont(Font)
        self.lblValu = QLabel(str(Value))
        self.lblValu.setFont(Font)
        
        HBox = QHBoxLayout()
        HBox.addWidget(self.lblText)
        HBox.addWidget(self.lblValu)
        HBox.addStretch(1)

        self.setLayout(HBox)
        
    def SetValue(self, Value):
        self.lblValu.setText(str(Value))

class HomePage(QWidget):
    def __init__(self):
      # Note unless you fully understand the 3 major issues you create by using
      # Super( ) you should not use it for the rather rare case it was designed for
        QWidget.__init__(self)  
        self.setWindowTitle('PySide2')
        Top = 200; Left = 700; Wdth = 250; Hght = 100
        self.setGeometry(Left, Top, Wdth, Hght)

        self.Hour = 0
        self.Mnut = 0
        self.Scnd = 0
        self.Cntr = 0

        self.btnMake = QPushButton('Update')
        self.btnMake.clicked.connect(self.Updater)

        self.btnHllo = QPushButton('Hello')
        self.btnHllo.clicked.connect(self.Holler)

      # Note one could also use a GridLayout as well
        HBox1 = QHBoxLayout()
        HBox1.addStretch(1)
        HBox1.addWidget(self.btnMake)
        HBox1.addWidget(self.btnHllo)
        HBox1.addStretch(1)

        self.lblMesg = QLabel('Text Message Goes Here')

        VBox1 = QVBoxLayout()
        VBox1.addWidget(self.lblMesg)
        VBox1.addLayout(HBox1)
        VBox1.addStretch(1)

        HBox2 = QHBoxLayout()
        HBox2.addLayout(VBox1)
        HBox2.addStretch(1)
 
        self.lblHour = DoubleLabel('Hours    :')
        self.lblMnut = DoubleLabel('Minutes :')
        self.lblScnd = DoubleLabel('Seconds :')

        VBox2 = QVBoxLayout()
        VBox2.addWidget(self.lblHour)
        VBox2.addWidget(self.lblMnut)
        VBox2.addWidget(self.lblScnd)
        VBox2.addWidget(QLabel(' '))
        VBox2.addLayout(HBox2)
        VBox2.addStretch(1)
        
        self.setLayout(VBox2)

    @Slot()
    def Updater(self):
        Today = dtDateTime.today()
        self.Hour = Today.hour
        self.Mnut = Today.minute
        self.Scnd = Today.second

        self.lblHour.SetValue(str(self.Hour))
        self.lblMnut.SetValue(str(self.Mnut))
        self.lblScnd.SetValue(str(self.Scnd))

    @Slot()
    def Holler(self):
        self.Cntr += 1
        Msg = 'Well Hello to you : '+ str(self.Cntr)
        self.lblMesg.setText(Msg)

if __name__ == '__main__':
    MainEvntHndlr = QApplication([])

    MainApp = HomePage()
    MainApp.show()

    MainEvntHndlr.exec()
Reply
#18
(Feb-19-2020, 06:55 PM)Denni Wrote: Okay while not perhaps exactly what you created it should give an idea of the how to -- also as I state in code you could have used a QGridLayout but I figured I would show the more free-form method as folks seem to have more trouble with that than using the more static Grid version. Also I did not implement the Message Box as there are plenty examples of how to do that out there and I was not sure what yours was supposed to be doing. I hope this helps you with understanding PySide2 (or PyQt for that mattter as I used PyQt to make this then just referenced it to PySide2)
from PySide2.QtCore    import Slot
from PySide2.QtGui     import QFont
from PySide2.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout
from PySide2.QtWidgets import QPushButton

from datetime import datetime as dtDateTime

class DoubleLabel(QWidget):
    def __init__(self, Text, Value=''):
        QWidget.__init__(self)

        Font = QFont()
        Font.setFamily('Helvetica')
        Font.setPointSize(12)
        Font.setBold(True)

        self.lblText = QLabel(Text)
        self.lblText.setFont(Font)
        self.lblValu = QLabel(str(Value))
        self.lblValu.setFont(Font)
        
        HBox = QHBoxLayout()
        HBox.addWidget(self.lblText)
        HBox.addWidget(self.lblValu)
        HBox.addStretch(1)

        self.setLayout(HBox)
        
    def SetValue(self, Value):
        self.lblValu.setText(str(Value))

class HomePage(QWidget):
    def __init__(self):
      # Note unless you fully understand the 3 major issues you create by using
      # Super( ) you should not use it for the rather rare case it was designed for
        QWidget.__init__(self)  
        self.setWindowTitle('PySide2')
        Top = 200; Left = 700; Wdth = 250; Hght = 100
        self.setGeometry(Left, Top, Wdth, Hght)

        self.Hour = 0
        self.Mnut = 0
        self.Scnd = 0
        self.Cntr = 0

        self.btnMake = QPushButton('Update')
        self.btnMake.clicked.connect(self.Updater)

        self.btnHllo = QPushButton('Hello')
        self.btnHllo.clicked.connect(self.Holler)

      # Note one could also use a GridLayout as well
        HBox1 = QHBoxLayout()
        HBox1.addStretch(1)
        HBox1.addWidget(self.btnMake)
        HBox1.addWidget(self.btnHllo)
        HBox1.addStretch(1)

        self.lblMesg = QLabel('Text Message Goes Here')

        VBox1 = QVBoxLayout()
        VBox1.addWidget(self.lblMesg)
        VBox1.addLayout(HBox1)
        VBox1.addStretch(1)

        HBox2 = QHBoxLayout()
        HBox2.addLayout(VBox1)
        HBox2.addStretch(1)
 
        self.lblHour = DoubleLabel('Hours    :')
        self.lblMnut = DoubleLabel('Minutes :')
        self.lblScnd = DoubleLabel('Seconds :')

        VBox2 = QVBoxLayout()
        VBox2.addWidget(self.lblHour)
        VBox2.addWidget(self.lblMnut)
        VBox2.addWidget(self.lblScnd)
        VBox2.addWidget(QLabel(' '))
        VBox2.addLayout(HBox2)
        VBox2.addStretch(1)
        
        self.setLayout(VBox2)

    @Slot()
    def Updater(self):
        Today = dtDateTime.today()
        self.Hour = Today.hour
        self.Mnut = Today.minute
        self.Scnd = Today.second

        self.lblHour.SetValue(str(self.Hour))
        self.lblMnut.SetValue(str(self.Mnut))
        self.lblScnd.SetValue(str(self.Scnd))

    @Slot()
    def Holler(self):
        self.Cntr += 1
        Msg = 'Well Hello to you : '+ str(self.Cntr)
        self.lblMesg.setText(Msg)

if __name__ == '__main__':
    MainEvntHndlr = QApplication([])

    MainApp = HomePage()
    MainApp.show()

    MainEvntHndlr.exec()

Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug [PyQt] Qt app won't crash after sys.excepthook override Alfalfa 3 2,653 Dec-28-2020, 01:55 AM
Last Post: deanhystad
  [PyQt] Customizing QMessageBox arbiel 1 6,234 Dec-16-2020, 08:57 PM
Last Post: Axel_Erfurt
  PY TO EXE CRASH kozaizsvemira 0 2,275 Oct-19-2019, 07:27 PM
Last Post: kozaizsvemira
  (pyQt/pySide)setStyleSheet(border…) makes QPushButton not clickable in Maya vladlenPy 0 4,727 Apr-15-2018, 12:41 PM
Last Post: vladlenPy
  [PyQt] SOLVED QMessageBox: can't get QMessageBox.Yes to work Fred Barclay 0 5,546 May-18-2017, 06:07 AM
Last Post: Fred Barclay

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020