Series of questions regarding python and panda

alright, now i am trying to add a general label text to the upper left of the screen… and make it so that the text is a string from a variable… here is my code, it needs some work, i know…

import direct.directbase.DirectStart
from direct.gui.OnscreenText import OnscreenText

class World:
  def __init__(self):
    self.title = OnscreenText(text="Hello World",
          style=1, fg=(1,1,0,1),
          pos=(0.8,-0.95), scale = .07)
    self.firstmessage = genLabelText(""+str(a)+"", 0)
          

w = World()
a = generaltext
run() 

im sure i just need to add a little…

here is my new code, it has gotten better, but for some reason, genLabelText is not working quite right…

here is the code…

import direct.directbase.DirectStart
from direct.gui.OnscreenText import OnscreenText

class World:

  def __init__(self):

    self.a = "generaltext"
    self.title = OnscreenText(text="Hello World", style=1, fg=(1,1,0,1),pos=(0.8,-0.95), scale = .07)
    self.firstmessage = genLabelText(self.a, 0)

w = World()

run() 

and here is the error message

DirectStart: Starting the game.
Warning: DirectNotify: category 'Interval' already exists
Known pipe types:
  wglGraphicsPipe
(all display modules loaded.)
:util(warning): Adjusting global clock's real time by 1.7643e-005 seconds.
Traceback (most recent call last):
  File "helloworld.py", line 12, in <module>
    w = World()
  File "helloworld.py", line 10, in __init__
    self.firstmessage = genLabelText(self.a, 0)
NameError: global name 'genLabelText' is not defined

**** End of process output ****

what do i need to change to get this to work?

I really recommend you to gain a bit more python experience before you start with panda, or you will run into these problems all the time.
genLabelText is a function which does not exist. You probably copied that code from some sample program which actually did define that function. If you did that you need to copy the genLabelText function as well.

This is more likely what you want to do:

import direct.directbase.DirectStart
from direct.gui.OnscreenText import OnscreenText

class World:
  def __init__(self):
    self.a = "Hello World"
    self.title = OnscreenText(text=self.a, style=1, fg=(1,1,0,1),pos=(0.8,-0.95), scale = .07)

w = World()
run() 

I have no idea why your other code was there, but it didn’t make much sense. It still doesn’t, since when you now change self.a, the text won’t actually change. Instead, you can now use self.title.setText() to change the text.
If you want to know more about onscreen texts, visit this manual page:
panda3d.org/manual/index.php/OnscreenText

I dont know if this is a glitch in python or panda but you cannot mix tabs with spaces in python…even if the tabs align with the spaces!!! I’m sure this isnt a glitch but i thought that “spaces” was not really detected by the compiler or rather it did not care if you tabbed or spaced as long as they were aligned. I guess not

i am studying both python and panda right now, and am actually learning pretty quick…

what i would like to do at this point, is make it so that the value of “a” changes upon a key press…

i would like to change the message upon pushing the up key…

what would i do to script that from this point?

after this, i will study up on python some more…

Try this:

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.gui.OnscreenText import OnscreenText

class World(DirectObject):
  def __init__(self):
    self.title = OnscreenText(text="Not pressed", style=1, fg=(1,1,0,1),pos=(0.8,-0.95), scale = .07, mayChange = True)
    self.accept("a", self.title.setText, extraArgs=["Pressed"])

w = World()
run() 

This makes the text change when the “a” key gets pressed.
Check for more info on this the manual section Events and the manual section for Keyboard input.

That is definately a Python thing. Python depends on indents to delimit code blocks, but tabs aren’t always worth the same number of spaces. The ratio can vary depending on system or application settings. That is why you should never use tabs in your Python code. Only use spaces. Many Python editors can automatically convert tabs to spaces as you type. IDLE, the editor that comes with Python, does this by default.

Each app has its own setting in translating a tab, including this board’s code form. That’s why sometimes a perfect copy paste from here doesn’t always correct.

ok, indentations aside, i would like to get back to my questions about how scripts work…

i saw this script that was posted by pro-rsoft in response to my question regarding onscreentext and key presses, variables and so forth…
here is what he said

now then,

my next question is how do i make it change back to its original message when i take my finger off the key?

Accept “a-up” event.

That would be very similar:

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.gui.OnscreenText import OnscreenText

class World(DirectObject):
  def __init__(self):
    self.title = OnscreenText(text="Up", style=1, fg=(1,1,0,1),pos=(0.8,-0.95), scale = .07, mayChange = True)
    self.accept("a", self.title.setText, extraArgs=["Down"])
    self.accept("a-up", self.title.setText, extraArgs=["Up"])

w = World()
run() 

As I said, this is explained in the Keyboard Input and Events sections of the manual.