I am working on UI window with a browse button. I have a browsePath function that I am calling in a cmds.button command. How can I get the output of browsePath() in my main function?
def main():
cmds.button(label='Browse', command=browsePath)
def browsePath(*args):
path = cmds.fileDialog2(fm=2)
if path:
cmds.textField('txt', tx=path[0])
return path
I don't think so maya gui events return anything, you probaly need to relay on a variable outside. But since you already getting the path value to your textField you can always query the textField value where you want.
Related
Hello I'm new to python and I'm stuck I need help.
My problem is this:
I have a function that returns a string
I call this function in a button
How to get this value ?
My code :
from tkinter import*
from tkinter import filedialog
file_path = ''
def window():
fen=Tk() fen.geometry('{0}x{1}+{2}+{3}'.format(600, 400, 300, 200))
fen.config(bg = "#87CEEB")
return fen
def select_file():
file_path = filedialog.askopenfilename()
return file_path
def main():
fen = window()
bouton1 = Button(fen, text = "Select a file", command= select_file())
print(file_path)
fen.mainloop()
main()
If I print the file_path in my functions
I can do it, it's perfect,
But I can't get it out of the function
You can't get the return value when the function is called as the result of an event (button press, etc). The code that calls the function (inside of mainloop) ignores the return value.
If other parts of the code need the value, you need to store it as an attribute of an object or as a global variable.
The other problem in your revised code is that you're calling the print statement before the user has a chance to click the button, so the value will be blank even if you use a global variable. You have to wait to use the value until after you've clicked the button and selected the file.
For example, create a function that prints the value, and then attach that function to a button. Click your button to choose the file, and then click the button to display the data. As long as you save the value in the first function into a global variable, it will be available in the second function.
You wont be able to do this as you are, in theory, returning the file path to the button.
Use the function to set the variable in an object, or set a global variable.
Additionally, you dont need the lambda before your function call, just call the function without the braces (i.e. command = select_file)
In the pythonista app for iOS I'm trying to check if a textfield in my ui has any numbers in it when I press on a button. When I use the same code on a string it works. When I use the code for textfield within a button's action it doesn't work. Please help? Here's what my code is like:
import ui
import console
def contains_digits(d):
for char in d:
if char.isdigit():
return True
return False
test='test3'
print(contains_digits(test)) #this works - it prints TRUE!
#below always prints failed even though it should print whatever is in the textfield?!
def button_pushed(sender):
if contains_digits(textfield1):
print(textfield1)
print('failed')
v=ui.load_view()
textfield1 = str(v['textfield1'].text)
v.present('fullscreen')
You’re setting the variable textfield to the field’s text before any text can be entered into the textfield1 TextField.
It will work if you just set textfield1 to v['textfield1'] and then get the .text out of it in your button_pushed function.
def button_pushed(sender):
if contains_digits(textfield1.text):
print(textfield1.text)
return
print('failed')
v=ui.load_view()
textfield1 = v['textfield1']
v.present('fullscreen')
This works because, instead of setting the text to what is in the field at startup, it waits until you have received notification that the field has been changed (assuming that is when button_pushed is called).
However, the best way to do this is to use the sender variable that button_pushed receives. Assuming you’ve set the TextField up so that textfield1.action is the button_pushed function, sender should be the TextField, so that you can do:
def button_pushed(sender):
if contains_digits(sender.text):
print(sender.text)
return
print('failed')
(Note that the way button_pushed is set up in your code, it will always print failed even if it also prints the semi-numeric text. You also need a return after the successful printing.)
I am aware of two ways to set a TextField to call a function (or method) as its action in Pythonista. The easiest is probably to set it within your code.
v=ui.load_view()
textfield1 = v['textfield1']
textfield1.action = button_pushed
v.present('fullscreen')
The other method is to set it within the UI file. When you have the TextField selected in the UI editor, and are looking at the settings for the TextField (as I write this, the “i” button in the upper right brings up the settings), the very bottom setting is “CUSTOM ATTRIBUTES”. Set the custom attributes to:
{'action':button_pushed}
Once you make either of these changes, then whenever the text field has finished editing (for example, by pressing “return” or by moving the focus to another field), that action will be called. Of course, if you do this and it turns out to be the behavior you want, you may wish to change the name of the function from button_pushed to something more descriptive of when or why the function is called.
You may also wish to change the name of the TextField from its default of “textfield1” to something more descriptive of its purpose as well, so as to make your code easier to follow once you have multiple fields to work with.
I have just now begun with GUI programming in Python 2.7 with Tkinter.
I want to have a button Browse, which when clicked opens the Windows File Explorer and returns the path of file selected to a variable. I wish to use that path later.
I am following the code given here. It outputs a window displaying 5 buttons, but the buttons do nothing. On clicking the first button, it doesn't open the selected file.
Likewise, on clicking the second button, the askopenfilename(self) function is called and it should return a filename. Like I mentioned, I need that filename later.
How to I get the value returned by the function into some variable for future use?
There is no point in using return inside a callback to a button. It won't return to anywhere. The way to make a callback save data is to store it in a global variable, or an instance variable if you use classes.
def fetchpath():
global filename
filename = tkFileDialog.askopenfilename(initialdir = 'E:')
FWIW (and unrelated to the question): you're making a very common mistake. In python, when you do foo=bar().baz(), foo takes the value in baz(). Thus, when you do this:
button = Button(...).pack()
button will take the value of pack() which always returns None. You should separate widget creation from widget layout if you expect to save an actual reference to the widget being created. Even if you're not, it's a good practice to separate the two.
I have following function in python -
def GetClipboardText():
text_obj = wx.TextDataObject()
rtext = ""
if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open():
if wx.TheClipboard.GetData(text_obj):
rtext = text_obj.GetText()
wx.TheClipboard.Close()
return rtext
It works well when I invoke this function from a UI callback handler such as button click (The UI is in wxPython). But if I invoke function directly in a script, the wx.TextDataObject() returns None and the function fails.
Questions -
What particular UI class is the dependency for the clipboard to work? Do I need to show a frame on screen? Is there a work around like creating an invisible frame? Is frame what the clipboard depends on or is it something else?
Is it possible to use clipboard in a command line app without GUI?
Try initializing wx.App in your script. Many wx classes require it.
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().