How to erase an old result when clicking on a wxpython - python

I have a wxPython script, which executes a process after clicking the button and display the process output in a wx.StaticText.
The problem is when I want to re-execute the process , and so I click on button and the old result is displayed (output of the first process); this old result is still displayed on the window, while I have added an instruction that normally will hide it:
Here is lines of my code:
def OnButton(self, e):
if self.res=="udpscan":
self.resultat1.SetLabel("")
ipad=self.ipadd.GetValue()
fromm=self.fr.GetValue()
too=self.to.GetValue()
ifc=self.intf.GetValue()
if len(ipad) <1 or len(too) < 1 or len(fromm) <1 or len(ifc) < 1:
wx.MessageBox('Please enter missing values','error',wx.OK)
else:
sp=subprocess.Popen(['python','udp_portscan.py',ifc,ipad,fromm,too],stdout=subprocess.PIPE)
text=sp.stdout.readlines()
text="".join(text)
self.resultat1.SetLabel(text)
I have tried to add other intructions to hide it but same problem.
Can you help me please. Thank you

sp seems to store all the previous output in sp.stdout.
There might be a way to clear it using .kill():
sp=subprocess.Popen(['python','udp_portscan.py',ifc,ipad,fromm,too],stdout=subprocess.PIPE)
text=sp.stdout.readlines()
sp.kill()

Related

Intermittent issues with pywinauto.type_keys() going out of focus

I have a piece of pywinauto code which attempts to delete an Item from an ItemList. Occasionally the type_keys() isn't working, as if the window is going out of focus. Interestingly enough, if I click on the Command Prompt window where the python code is running, this suddenly triggers the code to properly set focus on the application and continue running.
Here's an overview of the steps:
Opens an Open Diaglog window in certain application
(here its Tradestation's Development Environment)
Selects an item from the ItemList
Type DELETE key
(confirmation window pops up)
Select 'Yes' in the confirmation window
Most of the time this sequence works, except occasionally (maybe 1 out of every 5 or so tries) it fails where the DELETE key press isn't happening. The item is selected, but the window appears out of focus (or so it seems).
I've been chasing this issue for days/weeks, trying various different things, i.e. added delays, adjusted timeouts, adjusted pywinauto.Timings.fast/slow, tried setting focus in various places, etc... I also put the code into an infinite 'while' loop hoping that it would work on subsequent tries.
Interestingly enough, when it gets stuck, if I click on the Windows Command Prompt window where the python application is running, then suddenly the Open Diaglog box goes back into focus and continues to run successfully.
I'm looking for suggestions as to the root cause OR what else I could try.
Here's where the code has ended up:
strategy = self.window.child_window(title=name, control_type='ListItem')
Timings.slow()
while True:
# Issues where delete/confirm window not showing
# Try solving with SetFocus in case the open diag is going out of focus
_logger.info("Set focus")
handle = win32gui.FindWindow(None, OPEN_DLG_TITLE)
win32gui.SetForegroundWindow(handle)
# Highlight the strategy
_logger.info("Select Strategy")
strategy.select()
if strategy.is_selected() == 0:
time.sleep(2)
continue
_logger.info("Strategy selected")
# Delete it...
_logger.info("Type DELETE")
#strategy.type_keys('{DELETE}')
strategy.type_keys('{DELETE}', set_foreground=False)
# Delay in case return comes back too quickly
time.sleep(2)
# Click 'Yes' in Confirmation Window
_logger.info("Check confirmation window")
confirm_window = self.window.child_window(
title="Confirm file removal",
control_type="Window"
)
if confirm_window.exists() is False:
try:
_logger.info('... does not exist. Briefly wait 5 seconds')
confirm_window.wait('exists', timeout=5)
except pywinauto.timings.TimeoutError:
#breakpoint()
_logger.info('---- Retrying -----')
continue
assert confirm_window.exists() is True
break
Timings.fast()

Make Python script wait for button click ipywidgets

I'm sorry if this question has already been answered, but I couldn't find anything on here (except this one which hasn't been answered: link and one that doesn't quite answer the question i'm asking link
I'm creating a button with ipywidgets, but the script doesn't wait for the button to be clicked. Here's a sample of the code/what I'm trying to achieve:
button = widgets.Button(description = "Click")
output = widgets.Output()
display(button, output)
def on_button_clicked(b):
with output:
print("button clicked")
button.on_click(on_button_clicked)
print("Python is ignoring you")
If I run this, I'll just get "Python is ignoring you" before I click the button. I want it to display the button, wait for the person to click it, and only then execute the rest ("Python is ignoring you").
Does anyone have an idea of how I can make python wait for the button to be clicked?
Would appreciate any help! Thanks
I never used the ipywidgets, but the problem is on the last line
you're telling python to print "Python is ignoring you" and python is doing it.
It will not wait for the user to click the button,
because the print statement is out the function "on_button_clicked".(or any)
So just Put it in the function. (That print Statement)

Weird behavior - wxPython Events

I have this following piece of code. It is working sometimes, it is not the other time.
def OnReset(self, event):
self.reset_pump.Disable() # Disables the button so it is clicked
self.WriteToController([0x30],'GuiMsgIn') # Sends the reset command
self.flag_read.set()
self.tr.join()
time.sleep(2)
start = time.time()
self.offset_text_control.Clear()
print "Helloin reset"
self.gain_text_control.Clear()
self.firmware_version_text_control.Clear()
self.pump_rpm_text_control.Clear()
self.pressure_text_control.Clear()
self.last_error_text_control.Clear()
self.error_count_text_control.Clear()
self.pump_model_text_control.Clear()
self.pump_serial_number_text_control.Clear()
self.on_time_text_control.Clear()
self.job_on_time_text_control.Clear()
#self.MessageBox('Pump RESET going on Click OK \n')
# Having the above step is useful
print time.time() - start
#self.ser.close()
wx.CallLater(3000, self.CalledAfter, [event,])
def CalledAfter(self, event):
self.tr = threading.Thread(target=ReadData, name="ReadThread", args=(self.ser, self.flag_read))
self.tr.daemon = True
self.tr.start()
self.reset_pump.Enable()
What it does is When I click the Reset button on my GUI, it has to clear certain text fields on the GUI. It has to clear it, only after joining the self.tr thread.
Once it clears, it will execute the command wx.CallLater(3000, self.CalledAfter, [event,]). Which then starts a new thread again.
Apparently The .Clear() command is working very at a non consistent level, it is working some time, not working the other times, and working again.
Any idea why this might happen would be very helpful.
There seems to be a difference in the way SetValue and Clear update the windows. Calling SetValue("") seems to be a suitable workaround to this behavior.

input() bug on Skulpt?

Here's the code I'm using on Skulpt:
print('Intro text')
user_input = input('Prompt the user: ')
print(user_input)
The first thing that happens is an empty dialog box pops up, i.e. without the prompt. Only once something is entered in the dialog box does everything print to the output window:
Dialog box with empty text output window
Does anyone have any ideas?
(Right now, I'm using a workaround using time.sleep() before each input() -- ugh)

Some questions about switches in python

This is my first Question here at StackOverflow, so please be patient with me if some info isn't present or I missed something important, but anyways i'll do my best :)
Recently I started to code in Python2.7, so I'm not very good at it. While playing with PyGtk, PyGObject, Glade, etc I found something particular about switches (Haven't tried with any other widget, so I don't know if it happens somewhere else. Most likely it doesn't, I hope...)
I made a very basic GUI with a single "window" plus a "switch" using Glade
My objective was to deactivate switch after user tried to activate it if some exeption raised up before, something like:
Activate it --> * Found error --> * Deactivate it
I made some code, and after a while, I noted that THIS piece of code created a loop-like block, blocking GUI's window afterwards:
builder = Gtk.Builder()
window1 = builder.get_object('window')
switchie = builder.get_object('switchie')
switchie.set_active(False)
def Hi(switch, active):
print switchie.get_active()
switchie.set_active(not switchie.get_active())
switchie.connect("""notify::active""", Hi)
window1.set_position(Gtk.WindowPosition.CENTER)
window1.connect("delete-event", Gtk.main_quit)
window1.show_all()
If i'm right, "switchie.connect" links "switchie" object with "Hi" func whenever "switchie" gets clicked.
But if I execute this and try to turn switch on, GUI hangs up. I did try to execute this via script & command-line and adding the "print switch state", resulting in an endless loop (True & False)
I tried with many other funcs I made, but neither of them could solve this issue. In fact, this is the "essence" of all the other funcs I made.
Why does this happen?
Where's the loop?
Am I wrong in some line?
Help is appreciated!
(If you need to see the rest of my faulty funcs, just ask for 'em, but I don't think they'll help...)
You want to hook up the switch like this:
switchie.connect("""activate""", Hi)
This will only get called once for every time it is clicked. What you were doing is hooking up to the signal after it changed, so it was constantly changing, and never catching up. You will also want to change
def Hi(switch, active):
to
def Hi(switch, active = None):
for keyboard support.

Categories

Resources