Indeterminate Progress Bar not moving - python-3.x

I can not get the progress bar to move. Solutions offered in other post were very convoluted. The original task was loading a workbook with openpyxl, but I have substituted a 3 second delay which presents the same problem. update() does not move the progress bar. update_idletask() does not render the window correctly. mainloop() works, but does not stop working.
I am running python 3.7.9 on windows 10.
from tkinter import *
from tkinter import ttk
import time
# from openpyxl import load_workbook
class ProgressBarIn:
def __init__(self, title="", label="", text=""):
self.title = title
self.label = label
self.text = text
self.pb_root = Tk() # create a window for the progress bar
self.pb_label = Label(self.pb_root, text=self.label) # make label for progress bar
self.pb = ttk.Progressbar(self.pb_root, length=400, mode="indeterminate") # create progress bar
self.pb_text = Label(self.pb_root, text=self.text, anchor="w")
def set_up(self):
# titlebar_icon(self.pb_root) # place icon in titlebar
self.pb_root.title(self.title)
self.pb_label.grid(row=0, column=0, sticky="w")
self.pb.grid(row=1, column=0, sticky="w")
self.pb_text.grid(row=2, column=0, sticky="w")
self.pb.start()
self.pb_root.update()
# self.pb_root.update_idletasks()
# self.pb_root.mainloop()
def stop(self):
self.pb.stop() # stop and destroy the progress bar
self.pb_label.destroy() # destroy the label for the progress bar
self.pb.destroy()
self.pb_root.destroy()
def my_function():
pb = ProgressBarIn(title="hey now", label="hold on", text="hello there")
pb.set_up()
time.sleep(3)
wb = "hello in three seconds"
# wb = load_workbook(file_path)
pb.stop()
return wb
print(my_function())

This solution uses a global as a flag which communicates to the progress bar when to stop updating after the multithread has finished it's task. The WaitUp class creates an attribute called "self.i_waited_for_this" which holds the result of the multithread's task and will be called to retrieve it once the multithread has finished. The progress bar uses time.sleep() to pause between incrementing the value of the progress bar while the global flag variable is True and stops once line 19 is read and the variable is changed to False. Just to be clear, the progress bar is running on the main thread, not an additional thread since tkinter and multithreading don't mix.
from tkinter import *
from tkinter import ttk
from threading import *
import time
class WaitUp(Thread): # Define a new subclass of the Thread class of the Thread Module.
def __init__(self, secs=0):
Thread.__init__(self) # Override the __init__
self.secs = secs
self.i_waited_for_this = ""
def run(self):
global pb_flag # this tells the progress bar to stop
for i in range(self.secs): # print one number each second / replace with your task
print(i)
time.sleep(1)
pb_flag = False
self.i_waited_for_this = "it is done" # result of the task / replace with object or variable you want to pass
class ProgressBarIn:
def __init__(self, title="", label="", text=""):
self.title = title # Build the progress bar
self.label = label
self.text = text
self.pb_root = Tk() # create a window for the progress bar
self.pb_label = Label(self.pb_root, text=self.label) # make label for progress bar
self.pb = ttk.Progressbar(self.pb_root, length=400, mode="indeterminate") # create progress bar
self.pb_text = Label(self.pb_root, text=self.text, anchor="w")
def startup(self):
self.pb_root.title(self.title)
self.pb_label.grid(row=0, column=0, sticky="w")
self.pb.grid(row=1, column=0, sticky="w")
self.pb_text.grid(row=2, column=0, sticky="w")
while pb_flag: # move the progress bar until multithread reaches line 19
self.pb_root.update()
self.pb['value'] += 1
time.sleep(.1)
def stop(self):
self.pb.stop() # stop and destroy the progress bar
self.pb_label.destroy() # destroy the label for the progress bar
self.pb.destroy()
self.pb_root.destroy()
def my_function():
global pb_flag
pb_flag = True
t1 = ProgressBarIn(title="hey now", label="hold on", text="hello there")
t2 = WaitUp(secs=5) # pass the progress bar object
t2.start() # use start() instead of run() for threading module
t1.startup() # start the progress bar
t2.join() # wait for WaitUp to finish before proceeding
t1.stop() # destroy the progress bar object
return t2.i_waited_for_this
# delclare global variables
global pb_flag # this tells the progress bar to stop
print(my_function())

Related

wxPython threading - GUI freezes while doing an animation in GLCanvas

What is the proper way of refreshing a GLCanvas (with heavy 3D geometries) without freezing the GUI?
I'm trying to use a set of GUI elements (buttons, checkboxes) to control a GLCanvas which is refreshed to display a 3D animation. My problem is that the GUI elements are not responsive when the animation loop is on.
I've tried to launch the animation loop in three different ways:
Option 1: within a thread
Option 2: using wx.lib.delayedresult (most likely similar to thread)
Option 3: calling Refresh after the onDraw event
It seems I can get the Option 1 to work, but I need to introduce a sleep delay with time.sleep(xxx) in between the call to the Refresh of the canvas. Otherwise the GUI remains poorly responsive: resizing the window, will end up "graying" the GUI elements, clicking on the checkboxes will trigger events but not the "checking" of the box, the "mouseover" effect over buttons does not work, etc.
I enclose a small working example. In my actual application, I animate a 3D geometry using moderngl, and I noticed that the "sleep" time required varies depending on how heavy the geometry is (as opposed to this example which is extremely lightweight and works with a delay as small as 0.00001s). I'm wondering what I'm missing. Thanks!
import wx
from wx import glcanvas
import wx.lib.delayedresult as delayedresult
from OpenGL.GL import *
import OpenGL.GL.shaders
import time
import numpy as np
from threading import Thread
# --- Option 1: Thread
class TestThread(Thread):
def __init__(self, parent, canvas):
Thread.__init__(self)
self.parent=parent
self.canvas=canvas
self.start() # start the thread
def run(self):
print('Thread running... ')
while self.canvas.animate:
#time.sleep(0.01) # <<<<<<<<<<<< This line needed
self.canvas.Refresh()
print('Tread done ')
class OpenGLCanvas(glcanvas.GLCanvas):
def __init__(self, parent):
glcanvas.GLCanvas.__init__(self, parent, -1, size=(400,400))
self.context = glcanvas.GLContext(self)
self.SetCurrent(self.context)
self.init = False
self.animate = False
self.refreshAfter = False
self.t=0
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, event):
wx.PaintDC(self)
if not self.init:
self.InitGL()
self.init = True
self.OnDraw()
def InitGL(self):
glEnable(GL_DEPTH_TEST)
def OnDraw(self):
""" Called at every frame"""
glClearColor(0.1, 0.0, np.mod(self.t,1), 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
if self.animate:
self.t+=0.0005 # increment time
# ---- Option 3
if self.refreshAfter:
self.Refresh() # Trigger next frame
self.SwapBuffers()
# --- Option 2: delayed results
def onAnimDelayedEnd(self, thread):
""" Consumer """
print('Delayed result done')
jobID = thread.getJobID()
result = thread.get()
def onAnimDelayedStart(self):
print('Delayed result running... ')
while self.animate:
self.Refresh()
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.canvas = OpenGLCanvas(self)
# GUI
self.rot_btn = wx.Button(self, -1, label="Toggle animation", pos=(430, 10))
self.cbDo = wx.CheckBox (self, -1, label="Do something", pos=(430 ,100))
self.radDo = wx.RadioButton(self, -1, label="Do something", pos=(430,140))
self.radDo2= wx.RadioButton(self, -1, label="Do something", pos=(430,180))
# Binding
self.rot_btn.Bind(wx.EVT_BUTTON, self.toggleAnim)
self.radDo.Bind(wx.EVT_RADIOBUTTON, self.doSomething)
self.radDo2.Bind(wx.EVT_RADIOBUTTON, self.doSomething)
self.cbDo.Bind(wx.EVT_CHECKBOX , self.doSomething)
def toggleAnim(self, event):
if not self.canvas.animate:
self.canvas.animate = True
# --- Option 1: thread
TestThread(self, self.canvas)
# --- Option 2: delayed result
#delayedresult.startWorker(self.canvas.onAnimDelayedEnd, self.canvas.onAnimDelayedStart, jobID=1)
# --- Option 3: refreshloop
#self.canvas.refreshAfter=True
#self.canvas.Refresh() # set the canvas into an "infinite" refresh loop
else:
self.canvas.animate = False
def doSomething(self, event):
print('Do something')
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="My wx frame", size=(600,400))
self.Bind(wx.EVT_CLOSE, self.on_close)
self.panel = MyPanel(self)
def on_close(self, event):
self.Destroy()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame().Show()
app.MainLoop()

Python 3.5 tkinter confirmation box created progress bar and reads in csv not quite working

I'm realtively new to python and am making a GUI app that does a lot of file i/o and processing. To complete this i would like to get a confirmation box to pop-up when the user commits and actions. From this when clicking 'yes' the app then runs the i/o and displays a progress bar.
From other threads on here I have gotten as far as reading about the requirement to create an addtional thread to take on one of these processes (for example Tkinter: ProgressBar with indeterminate duration and Python Tkinter indeterminate progress bar not running have been very helpful).
However, I'm getting a little lost because I'm not activating the threaded process from the Main() function. So I'm still getting lost in how, and where, I should be creating the progress bar and passing of the i/o process to another thread (reading in a csv file here).
Here is my code and I would be very grateful for any help anyone can give me:
import tkinter as tk
import tkinter.messagebox as messagebox
import csv
import tkinter.ttk as ttk
import threading
class ReadIn(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Read in file and display progress")
self.pack(fill=tk.BOTH, expand=True)
self.TestBtn = tk.Button(self.parent, text="Do Something", command=lambda: self.confirm_pb())
self.TestBtn.pack()
def confirm_pb(self):
result = messagebox.askyesno("Confirm Action", "Are you sure you want to?")
if result:
self.handle_stuff()
def handle_stuff(self):
nf = threading.Thread(target=self.import_csv)
nf.start()
self.Pbar()
nf.join()
def Pbar(self):
self.popup = tk.Tk()
self.popup.title('Loading file')
self.label = tk.Label(self.popup, text="Please wait until the file is created")
self.progressbar = ttk.Progressbar(self.popup, orient=tk.HORIZONTAL, length=200,
mode='indeterminate')
self.progressbar.pack(padx=10, pady=10)
self.label.pack()
self.progressbar.start(50)
def import_csv(self):
print("Opening File")
with open('csv.csv', newline='') as inp_csv:
reader = csv.reader(inp_csv)
for i, row in enumerate(reader):
# write something to check it reading
print("Reading Row " + str(i))
def main():
root = tk.Tk() # create a Tk root window
App = ReadIn(root)
root.geometry('400x300+760+450')
App.mainloop() # starts the mainloop
if __name__ == '__main__':
main()
The statement nf.join() in function handle_stuff() will block tkinter's main loop to show the progress bar window. Try modify handle_stuff() as below:
def handle_stuff(self):
nf = threading.Thread(target=self.import_csv)
nf.start()
self.Pbar()
#nf.join() # don't call join() as it will block tkinter's mainloop()
while nf.is_alive():
self.update() # update the progress bar window
self.popup.destroy()

tkinter Text Widget duplicating when progress bar displayed

I have an app that displays progress messages via a Text widget, so that it will scroll if there are many lines. It also displays a 'Task' Progress Bar for each individual step, and a different 'Total' Progress Bar for the total completion.
My situation is that I want to have the progress bars not displayed if they have no 'content'. So neither of them at the start and end of the processing. The 'Total' progress bar would display once the first step is complete, and the 'Task' progress bar would be displayed if the task is long enough to warrant it.
It's a big app but here is a shortened version of what I have (still long though).
#!/usr/bin/python
# coding=utf-8
# Try to work with older version of Python
from __future__ import print_function
import sys
if sys.version_info.major < 3:
import Tkinter as tk
import Tkinter.ttk as ttk
else:
import tkinter as tk
import tkinter.ttk as ttk
#============================================================================
# MAIN CLASS
class Main(tk.Frame):
""" Main processing
"""
def __init__(self, root, *args, **kwargs):
tk.Frame.__init__(self, root, *args, **kwargs)
self.root = root
self.MW_f = tk.Frame(self.root)
# Progress Messages
self.PM_prog_msgs_lf_var = tk.StringVar()
self.PM_prog_msgs_lf = tk.LabelFrame(self.MW_f,
text=' Progress Messages: ',
relief='sunken')
# Progress Message text widget
self.PM_prog_msgs_max_frame_height = 6
self.PM_prog_msgs_line_count = 0
self.PM_prog_msgs_t = tk.Text(self.PM_prog_msgs_lf, height=self.PM_prog_msgs_max_frame_height, width=100)
self.PM_prog_msgs_t_vbar = ttk.Scrollbar(self.PM_prog_msgs_lf,
orient='vertical', command=self.PM_prog_msgs_t.yview)
self.PM_prog_msgs_t['yscrollcommand'] = self.PM_prog_msgs_t_vbar.set
self.PM_prog_msgs_t['state'] = 'disabled'
self.PM_prog_msgs_t['border'] = 3
self.PM_task_progbar_var = tk.IntVar()
self.PM_task_progbar = ttk.Progressbar(self.MW_f,
orient='horizontal',
mode='indeterminate',
variable=self.PM_task_progbar_var)
self.PM_progbar_var = tk.IntVar()
self.PM_progbar = ttk.Progressbar(self.MW_f,
orient='horizontal',
mode='determinate',
variable=self.PM_progbar_var,
maximum=4)
self.PM_go_btn = tk.Button(self.MW_f,
text='Go',
command=self.action_go_btn)
self.MW_stsbar_tvar = tk.StringVar()
self.MW_stsbar = tk.Label(self.MW_f,
textvariable=self.MW_stsbar_tvar)
# Grid the widgets
self.MW_f.grid()
MW_grid_row = 0
# Place into MW_f
self.PM_prog_msgs_lf.grid(row=MW_grid_row, column=0, sticky='we')
# Place into PM_prog_msgs_lf
self.PM_prog_msgs_t.grid(row=0, column=0)
self.PM_prog_msgs_t_vbar.grid(row=0, column=1, sticky='ns')
MW_grid_row += 1
self.PM_task_progbar_row = MW_grid_row
self.PM_task_progbar.grid(row=self.PM_task_progbar_row, sticky='we')
MW_grid_row += 1
self.PM_progbar_row = MW_grid_row
self.PM_progbar.grid(row=self.PM_progbar_row, sticky='we')
MW_grid_row += 1
self.MW_stsbar.grid(row=MW_grid_row, sticky='w')
MW_grid_row += 1
self.PM_go_btn.grid(row=MW_grid_row)
# Remove these until needed
self.PM_task_progbar.grid_remove()
self.PM_progbar.grid_remove()
self.PM_prog_msgs_t_vbar.grid_remove()
self.MW_dsp_stsbar_msg('Processing: Refer to progress message pane for more details')
# Window Displays Now
#=========================================================================================
# Functions used by window
def action_go_btn(self):
"""
"""
msg = 'Line 1\nLine 2'
self.PM_dsp_prog_msgs(msg, 2)
self.PM_task_progbar_update()
time.sleep(5)
self.PM_progbar_update()
self.PM_dsp_prog_msgs('Line 3', 1)
self.PM_task_progbar_update()
time.sleep(5)
self.PM_progbar_update()
msg = 'Line 4\nLine 5\nLine 6'
self.PM_dsp_prog_msgs(msg, 3)
self.PM_task_progbar_update()
time.sleep(5)
self.PM_progbar_update()
msg = 'Line 7\nLine 8\nLine 9'
self.PM_dsp_prog_msgs(msg, 3)
self.PM_task_progbar_update()
time.sleep(5)
self.PM_progbar_update()
self.PM_progbar.grid_remove()
return
def MW_dsp_stsbar_msg(self, msg):
"""
"""
# Display the statusbar
self.MW_stsbar_tvar.set(msg)
return
def PM_dsp_prog_msgs(self, msg, lines):
"""
"""
# Populate the message
self.PM_prog_msgs_t['state'] = 'normal'
self.PM_prog_msgs_t.insert(tk.END, ('\n'+msg))
self.PM_prog_msgs_t['state'] = 'disabled'
self.root.update_idletasks()
self.PM_prog_msgs_line_count += lines
# If its time display the vert scroll bar
if not self.PM_prog_msgs_t_vbar.winfo_viewable():
if self.PM_prog_msgs_line_count > self.PM_prog_msgs_max_frame_height:
self.PM_prog_msgs_t_vbar.grid(row=0, column=1)
self.root.update_idletasks()
# Show the last line.
self.PM_prog_msgs_t.see(tk.END)
self.root.update_idletasks()
return
def PM_progbar_update(self):
"""
"""
if not self.PM_progbar.winfo_viewable():
self.PM_progbar.grid()
# Remove the task progbar
self.PM_task_progbar.stop()
self.PM_task_progbar.grid_remove()
# Increment the progbar
self.PM_progbar.step()
self.root.update_idletasks()
return
def PM_task_progbar_update(self):
"""
"""
# Display if not already displayed
if not self.PM_task_progbar.winfo_viewable():
self.PM_task_progbar.grid()
self.root.update_idletasks()
# Step
self.PM_task_progbar.start()
self.root.update_idletasks()
return
# MAIN (MAIN) ===========================================================================
def main():
""" Run the app
"""
# # Create the screen instance and name it
root = tk.Tk()
app = Main(root)
root.mainloop()
root.quit()
# MAIN (STARTUP) ====================================================
# This next line runs the app as a standalone app
if __name__ == '__main__':
# Run the function name main()
main()
What happens is when the 'Task' progress bar is displayed the 'Progress Messages' label frame is duplicated, and moved down one line (see the screen shot)
Sorry, I cannot attach the screen shot. This is a text version of the screenshot!
Progress Messages ---------------------------------------
Progress Messages ---------------------------------------
Message Step 1
[Task Progress Bar ]
[Total Progress Bar ]
The display remains this way until the end. Even when the 'Task' progress bar is removed the duplication remains. The display does not add a third version when the 'Total' progress bar is displayed though.
Everything corrects itself at the end when both progress bars are removed.
All the widgets are gridded to the correct frame and the row/column seems to be correct, so where do I look to correct this.
Many thanks, and sorry this is all so long.
MacBookPro (2007), python 3.4, tkinter 8.5

How do I thread a single method on a PyQt GUI so that the rest of the GUI is still accessible?

Basically, when the Switch Button is pressed, which is connected to the reconfigure method, I want everything in the reconfigure method to run as a separate thread/process so the main GUI is still accessible and not being blocked. Below is a watered down version of my code.
import sys, time
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import *
#//Popup Class - Will appear when the Switch Button is pressed
class Popup(QWidget):
def __init__(self):
QWidget.__init__(self)
#//nothing here now, it will have a message telling user to wait while program is run
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
grid = QtGui.QGridLayout()
label_header = QtGui.QLabel("TEST RECONFIGURE")
font = label_header.font()
font.setPointSize(24)
label_header.setFont(font)
#//Creating Static Labels that will be placed in the GUI
label_1 = QtGui.QLabel("Menu 1:")
label_2 = QtGui.QLabel("Menu 2:")
label_spacer = QtGui.QLabel("")
label_cfg = QtGui.QLabel("Current Configuration: '/tmp/directory_here' ")
global comboBox1
comboBox1 = QtGui.QComboBox()
comboBox1.addItem("1")
comboBox1.addItem("2")
global comboBox2
comboBox2 = QtGui.QComboBox()
comboBox2.addItem("3")
comboBox2.addItem("4")
#//Switch Button!!!
global switchButton
switchButton = QPushButton("Switch")
switchButton.clicked.connect(self.reconfigure)
quitButton = QtGui.QPushButton('Quit')
quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
#//Configure the grid layout
grid.addWidget(label_spacer, 0,0,9,9)
grid.addWidget(label_header, 0,0,1,6)
grid.addWidget(label_1, 1,0,1,1)
grid.addWidget(comboBox1, 2,0,1,1)
grid.addWidget(label_2, 3,0,1,1)
grid.addWidget(comboBox2, 4,0,1,1)
grid.addWidget(switchButton, 5,0,1,2)
grid.addWidget(label_cfg, 6,0,1,9)
grid.addWidget(quitButton, 9,9,1,1)
self.setLayout(grid)
self.setGeometry(640,300,400,600)
self.show()
#//open up the popup window for switch button, and reconfigure
def reconfigure(self):
print "Opening New Window"
self.w = Popup()
self.w.setGeometry(QRect(self.x()+100,self.y()+100,400,200))
self.w.show()
txt1 = str(comboBox1.currentText())
txt2 = str(comboBox2.currentText())
print " reconfiguring to option %s and option %s" %(txt1, txt2)
#
# This is where most of the work is done, and takes about 1/2 an hour for everything to run
# Want to make this method a separate thread/process so the rest of the main GUI is still accessible
# while the program is running as the whole class will be a separate tab in a larger GUI
#
print "all done!"
#//runner
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I feel like I have to use threading and signals to accomplish this, but I am not having much luck. Any help would be greatly appreciated. Thank you!
import sys, time
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import *
class ConfigureThread(QtCore.QThread):
def run(self):
pass
#
# This is where most of the work is done, and takes about 1/2 an hour for everything to run
# Want to make this method a separate thread/process so the rest of the main GUI is still accessible
# while the program is running as the whole class will be a separate tab in a larger GUI
#
#//Popup Class - Will appear when the Switch Button is pressed
class Popup(QWidget):
def __init__(self):
QWidget.__init__(self)
#//nothing here now, it will have a message telling user to wait while program is run
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
grid = QtGui.QGridLayout()
label_header = QtGui.QLabel("TEST RECONFIGURE")
font = label_header.font()
font.setPointSize(24)
label_header.setFont(font)
#//Creating Static Labels that will be placed in the GUI
label_1 = QtGui.QLabel("Menu 1:")
label_2 = QtGui.QLabel("Menu 2:")
label_spacer = QtGui.QLabel("")
label_cfg = QtGui.QLabel("Current Configuration: '/tmp/directory_here' ")
global comboBox1
comboBox1 = QtGui.QComboBox()
comboBox1.addItem("1")
comboBox1.addItem("2")
global comboBox2
comboBox2 = QtGui.QComboBox()
comboBox2.addItem("3")
comboBox2.addItem("4")
#//Switch Button!!!
global switchButton
switchButton = QPushButton("Switch")
switchButton.clicked.connect(self.reconfigure)
quitButton = QtGui.QPushButton('Quit')
quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
#//Configure the grid layout
grid.addWidget(label_spacer, 0,0,9,9)
grid.addWidget(label_header, 0,0,1,6)
grid.addWidget(label_1, 1,0,1,1)
grid.addWidget(comboBox1, 2,0,1,1)
grid.addWidget(label_2, 3,0,1,1)
grid.addWidget(comboBox2, 4,0,1,1)
grid.addWidget(switchButton, 5,0,1,2)
grid.addWidget(label_cfg, 6,0,1,9)
grid.addWidget(quitButton, 9,9,1,1)
self.setLayout(grid)
self.setGeometry(640,300,400,600)
self.show()
#//open up the popup window for switch button, and reconfigure
def reconfigure(self):
print("Opening New Window")
self.w = Popup()
self.w.setGeometry(QRect(self.x()+100,self.y()+100,400,200))
self.w.show()
txt1 = str(comboBox1.currentText())
txt2 = str(comboBox2.currentText())
print(" reconfiguring to option %s and option %s" %(txt1, txt2))
self.th = ConfigureThread()
self.th.finished.connect(self.configuring_done)
self.th.start()
def configuring_done(self):
print("all done!")
#//runner
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

PyQt: How can I quit a QDialog?

I've build a QDialog Widget. My problem is, I can't quit the QDialog.
If I press one of the buttons, then the QDialog is only set to "hide".
Here is a little part of the code. It is executable.
I don't know what I'm doing wrong. Maybe one of you can tell me.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class MyClass(QDialog):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
# init
# ------------------------------------------------
self.setMinimumWidth(600)
self.setWindowTitle("Select Dingsda")
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.layoutWidget = QWidget(self)
self.liste = []
# widgets and layouts
# ------------------------------------------------
tempLayout = QHBoxLayout()
self.cancelButton = QPushButton("Cancel")
self.connect(self.cancelButton, SIGNAL('clicked()'), self.cancel)
self.addSelectedButton = QPushButton("Add Selected")
self.connect(self.addSelectedButton, SIGNAL('clicked()'), self.addSelected)
tempLayout.addStretch()
tempLayout.addWidget(self.cancelButton)
tempLayout.addWidget(self.addSelectedButton)
self.layout.addLayout(tempLayout)
# test-data
# ------------------------------------------------
# methods
# ------------------------------------------------
def cancel(self):
self.close()
def addSelected(self):
self.liste = ["1", "2", "3", "4", "5"]
self.accept()
def exec_(self):
if QDialog.exec_(self) == QDialog.Accepted:
return self.liste
else:
return []
def test():
app = QApplication([""])
form = MyClass()
i = form.exec_()
print i
sys.exit(app.exec_())
#-------------------------------------------------------------------------------
# main
#-------------------------------------------------------------------------------
if __name__ == "__main__":
test()
To terminate a dialog, accept should work (at least if you've made your dialog modal, which I believe exec_ always does).
The normal alternative is reject; or, instead of either or both, you could call done with an int parameter (which becomes exec_'s result).
I don't know python at all but it looks like the dialog is the only window for your app. You may want to try invoking the dialog with form.show_() instead of form.exec_(). The latter is normally used to display the dialog modally over a parent window.

Resources