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.
Related
I have a code snippet which runs perfectly. In some cases, I need user input, but there are also cases where user input is not necessary and code functions without it perfectly.
So, in that cases I create with conditional a flow where entry box widget is created and destroyed after value is get() by script. But I cannot make code to wait until to say stops(pauses) when the user has given input value then continues to run.
code is below;
varSheetname_GS = ''
if varsoundTitle_usernameHeroContainer == 'FloatingBlueRecords' or varsoundTitle_usernameHeroContainer == 'DayDoseOfHouse':
varSheetname_GS = varsoundTitle_usernameHeroContainer
else:
# look for sheetname as an input value entered by user
new_sheetname_entryBox=tk.Entry(canvas2,width=30).pack()
new_sheetname_entryBox.focus()
var_new_sheetName =new_sheetname_entryBox.get()
new_sheetname_entryBox.destroy()
varSheetname_GS = var_new_sheetName #input("Enter the sheetname in (GooSheets):")
I have looked for so_01 and so_02 which are related to topic but was not able to implement in my situation. So, anyone who would guide me towards that answers would be great from yourside.
Thanks ahead!
You can use the wait_window method to wait until the entry widget has been destroyed. You can then bind the return key to destroy the window. If the entry is associated with a StringVar, you can get the value after the widget has been destroyed.
The solution might look something like this:
entry_var = tk.StringVar()
new_sheetname_entryBox=tk.Entry(canvas2,width=30, textvariable=entry_var)
new_sheetname_entryBox.pack()
new_sheetname_entryBox.bind("<Return>", lambda event: new_sheetname_entryBox.destroy())
new_sheetname_entryBox.focus_set()
# wait for the entry widget to be deleted...
new_sheetname_entryBox.wait_window()
# save the value
varSheetname_GS = entry_var.get()
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)
I've inserted a simple QTextEdit widget into my PyQt user interface. When the user wants to type text into that widget, he has to click on it. My program should be able to make this happen automatically at certain occasions, such that the user can start typing text into that QTextEdit widget without the need for clicking on it.
I already got somewhere, but the issue is still not solved completely. When my program calls the focus() function, the cursor will start blinking at the end of the last line. But typing on your keyboard doesn't insert any text.
class myTextField(QPlainTextEdit):
def __init__(self):
super(myTextField, self).__init__()
...
def focus(self):
self.focusInEvent(QFocusEvent( QEvent.FocusIn ))
# Now the cursor blinks at the end of the last line.
# But typing on your keyboard doesn't insert any text.
# You still got to click explicitly onto the widget..
...
###
Any help is greatly appreciated :-)
Use setFocus() method.
def focus(self):
self.setFocus()
I'm trying to work on a GUI done in pyqt. I'm trying to create a pop-up window has a textbox where a user can type/set the user's id (1-99) and then click an 'ok' button to set it and close the window.
This is what I have so far.
def viewProfile(self)
profBox = QMessageBox()
QMessageBox.about(self, 'Profile', "///Text box where can type User ID:// ",
QMessageBox.Ok)
I am not sure how to generate the textbox.
Also, if I want to display the integer value or string of a variable in my message window /box do I just leave it out of quotation marks but include it? What's the syntax for it?
Thank you!
You want to use a QInputDialog. This has a bunch of static methods which generate a complete dialog, and return the selected integer when the user clicks OK. This means you don't need to worry about creating a dialog object, adding widgets and buttons, etc.
So you would want to call:
parent_window = self #probably..., depends on your code
minimum_value = 1
maximum_value = 99
default_value = 1
title = "Profile"
message = "Select your user ID"
user_id, ok = QInputDialog.getInt(parent_window, title, message, default_value, minimum_value, maximum_value)
When the QInputDialog line of code runs, a dialog will be presented to the user. When the user clicks OK or Cancel, the entered user_ID will be placed in user_id and ok will be a boolean value that indicates whether the OK button was clicked (True if the OK button was clicked, False if the cancel button was clicked)
If you want to place an integer in the message, you could do something like:
message = "Select your user ID. An integer I want you to know about is %d. I hope you find that useful."%my_integer
But that is really a Python string formatting question, which you should research separately. In short, in my example you can display one string. How long, that string is, is up to you (it can be multiple lines, have new line characters, etc)
You should use QDialog. That way you can customize it the way you want (add textbox, button...)
Take a look at my answer here, basically it's log-in dialog created in QTDesigner, but you can create it with code since it's way more simplier
I am working on a PyGTK GUI project, and i would like to know, is there any way to get variable value from gtk.Entry?
text=gtk.Entry(0)
text.set_activates_default(True)
port = text.get_text()
This doesn't work as, the text box is empty by default, and get_text() returns empty string, any ideas?
This doesn't work as, the text box is empty by default, and get_text() returns empty string, any ideas?
Sounds like you're looking for getting the text after some user input. In order to do this, gtk uses signals that allow you to connect a user action to some function that will do something. In your case you want this function to get the text from the input. Because you haven't described the user interaction I'll give the simplest example. If you had a button
in your GUI that, when clicked, would grab whatever is typed in the entry at that moment, you'd do this:
button = gtk.Button( 'Click Me')
button.connect( 'clicked', on_button_click )
Then, you define the on_button_click function:
def on_button_click(self, widget, data=None):
port = text.get_text()
print 'Port: %s' % port
So with the sample code above, you'd have a button that, when clicked, grabs the text from your gtk.Entry.
Check out this link for a simple example on how to use signals in pygtk
Since your text field is by default empty, get_text() would return empty string.
Add a button callback function or some other function. So whenever that function is called, you would get the string from the Entry using
text.gtk_text()
You might want to use self here. since you'll be accessing entry in some other function and not in main thread, use
self.text=gtk.Entry()
self.text.set_activates_default(True)
def foo(self,widget,event): # some callback function
port = self.text.get_text()
print port
The retrieving method is the correct one, but right after the creation, the user can't yet have typed anyting into the field.
You would have to wait for the user to actually type something into the field, and afterwards calling get_text().