Error in python call function

Sciter Forums Sciter and C# | Rust | Go | Python Sciter/Python Error in python call function

Viewing 9 reply threads
  • Author
    Posts
    • #57423
      moncef_el
      Participant

      this is the error that i get when i try to call a python function with two arguments from tiscript:

      Traceback (most recent call last):
        File "C:\Users\Moncef\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sciter\event.py", line 171, in _on_script_call
          args = sciter.Value.unpack_from(f.argv, f.argc)
        File "C:\Users\Moncef\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sciter\value.py", line 645, in unpack_from
          return [value(args[i]).get_value() for i in range(count)]
        File "C:\Users\Moncef\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sciter\value.py", line 645, in <listcomp>
          return [value(args[i]).get_value() for i in range(count)]
        File "C:\Users\Moncef\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sciter\value.py", line 539, in get_value
          raise TypeError("%s (%s) is unsupported python type" % (str(t), str(u)))
      TypeError: VALUE_TYPE.T_OBJECT (UT_OBJECT_NATIVE) is unsupported python type
      error:tis: Error: VALUE_TYPE.T_OBJECT (UT_OBJECT_NATIVE) is unsupported python type
      	at @47@35 (file://C:/Users/Moncef/Desktop/pysciter-master/examples/my/main.htm(50))

      the code executed is:

      import sciter
      
      class Frame(sciter.Window):
      	def __init__(self):
      		super().__init__(ismain=True, uni_theme=True)
      		pass
      	@sciter.script
      	def call(self):
      		return "Pythonic window" 
      	@sciter.script
      	def Say(self,tex1,text2):
      		if (code == (sciter.event.BEHAVIOR_EVENTS.BUTTON_CLICK | sciter.event.PHASE_MASK.SINKING)) and he.test('#sub'):
      			return "Hello {} {}".format(text1,text2)
      
       
      
      if __name__ == '__main__':
          import os
          htm = os.path.join(os.path.dirname(__file__), 'main.htm')
          frame = Frame()
          frame.load_file(htm)
          frame.run_app()

      with main.htm:

      <script type="text/tiscript">
      view.caption = $(head > title).value;
      $(button#sub).on("click", function() {
        var p1 = self.select("#input-1");
        var p2 = self.select("#input-2");
        var ans = view.Say(p1,p2);   
        $(body).$append(<h1>script -> python: {ans}</h1>);
      })
      </script>
      </head>
      <body>
      <form #num1>
              <label>First name:</label>
              <input#input-1 novalue="Enter the first name">
              <label>Last name:</label>
              <input#input-2 novalue="Enter the last name">
              <footer>
                <button|submit #sub>Submit</button>
                <button|reset>Reset</button>
              </footer>
            </form>
      
    • #57424
      Andrew
      Keymaster

      These two variables:

      var p1 = self.select("#input-1");
      var p2 = self.select("#input-2");

      will contain references to DOM elements. But your code expects values. So

      1. Change your python code to this:

      def Say(self,tex1,text2):
        return "Hello {} {}".format(text1,text2)

      And 2. Sciter script to this:

      $(button#sub).on("click", function() {
        var p1 = self.select("#input-1").value; // note 'value' but not DOM element reference
        var p2 = self.select("#input-2").value;
        var ans = view.Say(p1,p2);   
        $(body).$append(<h1>script -> python: {ans}</h1>);
      })
      
    • #57425
      pravic
      Moderator

      var p1 = self.select(“#input-1”);

      It can be written as var p1 = $(#input-1) if you wish.

      view.Say(p1,p2)

      p1, p2 are elements, not values. Use view.Say(p1.value, p2.value) instead.

      if (code == (sciter.event.BEHAVIOR_EVENTS.BUTTON_CLICK

      Where does the code come from? And also tex1 vs text1 typo.

    • #57429
      moncef_el
      Participant

      Thank you for your response; the program works fine but when it comes to:
      $(body).$append(<h1#test>script -> python: {ans}</h1>);
      it doesn’t show the result returned from python, in my opinion I think that I should add an view.caption somewhere in my tiscript to show the returned message from Python.
      I can see clearly that python received the variables and did the work fine when i turn return to print and that’s because of your help, Thanks! but I still want to show the returned value into the window.

    • #57431
      pravic
      Moderator

      Try to add some logging in order not to be blind (check https://sciter.com/revising-assert-and-debug-statements/).

      Use stdout.println or stdout.printf("ans is %v\n", ans) (\nis important!) or debug: ans.

    • #57432
      pravic
      Moderator

      And I hope you use the correct Python code:

      
      import sciter
      
      class Frame(sciter.Window):
      	def __init__(self):
      		super().__init__(ismain=True, uni_theme=True)
      		pass
      
      	@sciter.script
      	def call(self):
      		return "Pythonic window" 
      
      	@sciter.script
      	def Say(self, text1, text2):
      		print("Say(%s, %s)" % (text1, text2))
      		return "Hello {} {}".format(text1, text2)
      
      if __name__ == '__main__':
          import os
          htm = os.path.join(os.path.dirname(__file__), 'main.htm')
          frame = Frame()
          frame.load_file(htm)
          frame.run_app()
      
      
      view.caption = $(head > title).value;
      $(button#sub).on("click", function() {
        var p1 = $(#input-1).value;
        var p2 = $(#input-2).value;
        stdout.printf("calling view.Say(%v, %v)\n", p1, p2);
      
        var ans = view.Say(p1, p2);   
        stdout.printf("the answer is: %v\n", ans);
      
        $(body).$append(<h1>script -> python: {ans}</h1>);
      })
      
      • This reply was modified 7 years, 5 months ago by pravic. Reason: Corrected selectors
    • #57433
      moncef_el
      Participant

      Thank you #57432, this was helpful to understand things for me, but what i’m really looking for is to display the result into the frame (I mean the window) because the $(body).$append(<h1>script -> python: {ans}</h1>); don’t do anything. Your code was good and fine but only the $(body).$append(<h1>script -> python: {ans}</h1>);

    • #57434
      Andrew
      Keymaster

      This:

      var p1 = $("#input-1").value;
      var p2 = $("#input-2").value;

      shows error in debug console – “bad selector”

      Instead, you should use this:

      <html>
          <head>
              <title>Test</title>
              <style></style>
              <script type="text/tiscript">
      
      // emulation of corresponding method defined in Python:
      function view.Say(text1,text2) {
        return String.$(Hello {text1} {text2});
      }
      
      $(button#sub).on("click", function() {
        var p1 = $(#input-1).value;
        var p2 = $(#input-2).value;
        stdout.printf("calling view.Say(%v, %v)\n", p1, p2);
      
        var ans = view.Say(p1, p2);   
        stdout.printf("the answer is: %v\n", ans);
      
        $(body).$append(<h1>script -> python: {ans}</h1>);
      })        
              </script>
          </head>
          <body>
            <input|text #input-1 />
            <input|text #input-2 />
            <button#sub>Test</button>    
          </body>
      </html>

      While testing in Python remove function view.Say(text1,text2) definition from the above.

    • #57435
      moncef_el
      Participant

      Thank you Andrew this code is working good!

    • #57438
      pravic
      Moderator

      My bad, used the wrong selector in html.

Viewing 9 reply threads
  • You must be logged in to reply to this topic.