Execute a function after calling gtk.main - python

Here is my dilemma: I have a log parser with a pygtk window. After the user opens a file via the dialog, I have a progress bar showing them approximately when the open will be complete (as it can be a lengthy process). I recently added an option to allow the user to specify a file via the command line.
My problem is, since I have to load the file before calling gtk.main(). Thus, the window and, more importantly, the progress bar are not displayed until after this lengthy load completes, giving the user no indication.
So, I am hoping there is a way to execute a function after calling gtk.main(), or somebody has an alternative approach. Thanks

Rather than beginning the process from the command line as soon as you parse the command line arguments, simply pass the command line argument to your application class (or store in a variable) and process it AFTER you've entered gtk.main. That way you can setup your GUI first. Before beginning the process, and every time you increment the progress bar, make sure you let Gtk catch up using something like:
while Gtk.events_pending():
Gtk.main_iteration()

Related

How to execute a command with a button and delete it before the command ends ? Python / Tkinter

I've got an interface where there is a 'Start' button. It runs a 'main' command where it starts a loop to run some measurements on a powermeter. I want to be able to click on an 'OK' button every time the measure is ready to be done. This button would replace the 'Start' button.
But when I try to destroy the button (buttonStart.destroy()) and then run the command main()
, the command executes but doesn't delete the button until the very end.
I've tried using threads from threading package, but it doesn't really work.
If there is a way to both destroy the button and run a command, I would be very interested !
Thanks a lot
The event loop must be given the opportunity to process events in order for the window to be removed from the screen.
There are a couple of ways to make that happen. The first is to call the update_idletasks method on any widget. That is usually enough to process the event related to the destruction of the widget, though it may be necessary to call update instead. Calling update should be avoided if at all possible. My rule of thumb is to start with update_idletasks. If that doesn't work, switch to update or switch to using after.
def my_custom_function():
startButton.destroy()
root.upddate_idletasks()
main()
The second method is to run your main function via after or after_idle. This will let tkinter naturally process all pending events before starting main. I would argue that this is the best approach. I recommend trying after_idle first, and if that doesn't work, switch to after with a small delay.
def my_custom_function():
startButton.destroy()
root.after_idle(main)

Python3 - Issues with "subprocess.call()" function

my software uses the subprocess.call([sys.executable, SCRIPT_NAME] instruction in order to open others kind of scripts specified by the user using a GUI (Tkinter). I have two issue with this instruction:
the "command line" scripts start and close themeselves quicly and it means that the user can't interact with them. it's a weird behaviour because in all of them there is an input instruction, so they should wait an input by the user before to close themeselves. how can I solve this issue?
the "GUI" scripts instead, start without any kind of issue, but their "life", let me say, put in stuck the main script (it uses Tkinter). in this case I can interact with the second script but not with the main one. how can I call my other scripts with the subprocess.call() function whithout put in stuck the main one? from my point of view this issue happens because the second script is a part of the same process of the main one and in this case Tkinter has to wait. if we open the others scripts using different processes for all of them the main script would be free to live its life independently of the others. but how can I do it?

is it possible to launch a window that does not stop the mainloop in python

OS = windows 7
I have a python program (works) that is listening to activity on the usb bus. I want to perform a lot of tests that require a particular user input at a particular time. I would like to pop up a window that says, "press button xxx". The key point is that the mainloop needs to continue running because it's looking for events. I don't care about the window or if it remains or not and I don't need to capture any information from the window. I just want a message to the user to press the correct button at the correct time. Any type of signaling would work; it doesn't have to be a gui window. It doesn't have to look pretty. Appreciate any suggestions or links to something like this. thx
It sounds like the operation of the Python script you're running does not depend upon the user input you request. To run another process without interrupting the Python script execution you can use:
import subprocess
subprocess.Popen([exe,arg1,arg2,arg3])
where
exe = executable/script to run from your OS command line
arg1= first argument to pass to exe
arg2= second argument to pass to exe
etc... (as many arguments as your OS supports in a list)
This separate exe process could request input from the user.

Python: Redirect output to several consoles?

I have a main program, in which a user can call a sub-process (to download files) several times. Each time, I call aria2c using subprocess, and it will print the progress to stdin. Of course, it is desirable that the user can see the progress of each download seperately.
So the question is how can I redirect the output of each process to a seperate console window?
I'm a bit confused. Using subprocess.Popen(...) should spawn a new command prompt automatically for each call. What is aria2c? Is it a program you had written in python as well? Is it a 3rd party exe that writes to the command prompt window?
I can help you to redirect all the sub-processes output to the main command prompt, so it can be displayed inline.
Also, maybe you can give a little more detail on what is going on first, so I can understand your trouble a bit better.

Is there a wxpython event like program_start?

OK, I'm trying to explain what I want to achieve in another way. Here's an example:
Say if it's an anti virus program, and user can choose between two ways to run the program, choice one, automatically start to scan disks for virus when the program starts up, choice two, hit the start button to make the program scan disks for virus after the program starts up any time the user wants. So, as a wxpython beginner, I know how to bind wx.EVT_BUTTON to let scanning start after the user hit the start button, but I don't know how to make the scanning start once the program starts up. I wonder if there's a program_start event I can bind? Hope you guys can help me out. Thanks!
In wxPython you can override the OnInit method of your Application class to run code when the program launches. For example:
def OnInit(self):
# Check for a running instance for this user. Do not instantiate if found.
if self.checkInstance():
dbcon.cursor().callproc('post_mutex', (self.mutexname,))
dbcon.commit()
self.Cleanup()
return False
# Register for database events.
DataCache['dbListener'] = dbListener()
return True
There is of course another method on my Application class called checkInstance. Depending on it's return value, my application either launches, or triggers the other running instance to launch.
In wxPython you don't have to do anything special with your App class to get the binding to take place for your OnInit method. It'll happen automatically if you override it.
Why don't you run it just in module code? This way it will be run only once, because code in module is run only once per program instance.
In your init or OnInit method, do some kind of check to see if the program should run the startup process on startup (i.e. check a config file or some such). If yes, call the "scan" method using wx.CallAfter or wx.CallLater or call it after you Show() the frame.

Categories

Resources