Home Technical Talk

Maya python: button calling a function

dlz
dlz
polycounter lvl 4
Offline / Send Message
dlz polycounter lvl 4
Anyone knows how to call a function with a button inside a class? 

at the moment i have that code:

#button calling functions
import maya.cmds as cmds
import maya.mel as mel
from functools import partial

class B:
    text_entered="a"
    def __init__(self):
        self.create_window()

    def printText(self):
        print(self.text_entered)
        return

    def create_window(self):
        window = cmds.window()
        cmds.rowColumnLayout(numberOfColumns=2, columnWidth=[(1, 50), (1, 70)])
        cmds.text(label='Name')
        axis = cmds.textField()
        cmds.showWindow(window)
        self.text_entered = cmds.textField(axis, text=True, query=True)
        cmds.button(label="Apply", command = partial(self.printText))
        return       

B()

But i get that error
# Error: printText() takes exactly 1 argument (2 given) # 

Any ideas?

Replies

  • EosOfOrcus
    Offline / Send Message
    EosOfOrcus polycounter lvl 4
    What short of script are you trying to write?

    when you write print(self.text_entered) in this statement it seems like there is 2 arguments or it doesn't return self.text_entered because the def create_window(self) doesn't store (self.text_entered) . maybe try print(self)

    sorry my scripting is a bit rusty
  • IacopoAntonelli
    Offline / Send Message
    IacopoAntonelli polycounter lvl 3
    Try
      def printText(self, *args):
  • dlz
    Offline / Send Message
    dlz polycounter lvl 4
    Sorry i was outside for a few days and i didnt has internet connection.
    I finally ended with a solution of make a function inside a function:
    import maya.cmds as cmds
    
    class B:
        def __init__(self):
            self.create_window()
    
        def create_window(self):
            if cmds.window("UI", exists=True):
                cmds.deleteUI("UI")
            win = cmds.window("UI")
            cmds.columnLayout()
            textEntered = cmds.textField()
    
            def print_text_contents(a):
                print cmds.textField(textEntered, query=True, text=True)
    
            cmds.button(label='Confirm', command=print_text_contents)
            cmds.showWindow(win)
    
    
    B<span>()
    </span>
  • dlz
    Offline / Send Message
    dlz polycounter lvl 4
    IacopoAntonelli i tried that too but it printed nothing i supose because it gets the text before i entered a text in the textField
Sign In or Register to comment.