input() bug on Skulpt? - python

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)

Related

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)

Python tkinter button lagging/printing upon exit

First of all note that I am doing all these in Enthought/Canopy.
I have this basic GUI where it has 4 entry boxes in which you type stuff, and then you hit a button and it prints the stuff you entered. However, it is not operating the way I want it to. When you start the code and type in values and press the button, it won't do anything, then you close the gui window and it will print the values you entered.
Also, when you entered the values and hit the button once, again it won't do anything, but if you hit the button again it will print the values but not as expected. Say you entered 1, 2, 3, 4 and hit the button twice, the thing you see on the screen is 1, 2, 3, 4, 1 and when you close the window now, it will print out the rest.
I'd appreciate if you people can help me out with this. Thank you in advance. Below is my code:
Update: This issue does not happen with IDLE, but only Canopy.
from Tkinter import *
class Application:
def printcmd(self):
print(self.entrybox.get())
def __init__(self, master):
self.entrybox = Entry(master)
self.button = Button(master, text="print", command = self.printcmd)
self.entrybox.grid()
self.button.grid()
root = Tk()
Application(root)
root.mainloop()
If you are doing this in the Canopy GUI, you should ensure that Qt is not already set as the GUI backend. See https://support.enthought.com/hc/en-us/articles/204469880-Using-Tkinter-Turtle-or-Pyglet-in-Canopy-s-IPython-panel
For the print lag: Python buffers its output. If you want to ensure that some output is printed immediately, follow the print statement with sys.stdout.flush() to flush the print output buffers. (Of course you must first import sys.)
This can be an issue in any Python program. It arises more frequently in Canopy than in IDLE because Canopy used IPython's QtConsole which separates the execution kernel from the front end terminal-like panel into two separate OS processes.

Using pop up windows in tkinter

Here is a part of the script that I am using and it is working correctly.
try:
(sshin1, sshout1, ssherr1) = proxy_client.exec_command(mycommand)
print sshout1.read()
mytext=sshout1.read()
print type(mytext)
except:
print "error while executing the command on remote host"
But when I add this line, at the end, it creates a pop-up window with "Say Hello" title but does not show the mytext contents.
tkMessageBox.showinfo("Say Hello", mytext)
The print statement in the try block will display the output to the terminal. How do I show the same output in a pop-up window?
update:
The text shown in the pop-up window can not be copied and pasted to some other application like notepad. Is there any easy way?
I just had to comment out the print statement. For some reason the .read() output was getting destroyed after printing.
#print sshout1.read()

How to erase an old result when clicking on a wxpython

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()

QLineEdit can't get anything

i using pyqt develop a dialog, and a LineEidt
some like below, but i can't get anything from lineEdit1:
lineEdit1 = QtGui.QLineEdit()
lineEdit1.setEchoMode(2)
passWord = lineEdit1.text()
lineEdit1 = QtGui.QLineEdit()
lineEdit1.setEchoMode(2)
passWord = lineEdit1.text()
of course you can't get anything, because when lineEdit1.text() is executed, I believe there is no characters input into lineEdit1.
You should call passWord = lineEdit1.text() by some action when the input is over, for example, click a button.
You can execute your code on the editingFinished() signal of QLineEdit. It will be executed when the QLineEdit loses focus.

Categories

Resources