TkInter

Hello

I am trying to develop an application in panda3D and i want to use tkinter for my interface. i can’t actually make these two interact… How can i make a tkinter program call some panda3d functions??
Anyone can help?

Hi,

Panda has builtin support for Tk. You just have to place this in your Config.prc file:

want-tk #t

Alternatively, you can place this line after the DirectStart import:

base.startTk()

This enables all that’s needed to have Tk and Panda run together seamlessly. Now, you can just create windows, buttons, etc.

Are you sure you want to use tkinter though? I found the interface and api rather ugly. wxPython is better – and Panda also has support for that.

Hi

Thx for the prompt response! Actually i thought about wxpython before…but isn’t it more difficult to code?
And if i do use it how do i integrate them?

Cheers
Annie

If you want to use wxPython, you have to replace “want-tk” with “want-wx” (or [size=92]base.startTk()[/size] with [size=92]base.startWx()[/size])

After you’ve done that, you don’t have to create your own wx app anymore or run MainLoop(). You can just specify base.wxApp whenever you need to give an app somewhere.

Is there any example on it?

bump… I’m still scratching my head how to do this too…

when I use base.startTK(), and try to run it this error pops up:
“ModuleNotFoundError: No module named ‘Pmw’.”

I don’t have this DirectStart import though, I have a class that inherits the ShowBase class:


class OrbitalWindow(ShowBase):
    def __init__(self):
        super().__init__(self)
        base.startTk()
        self.system = SysProp()
        self.scrConfig()
        self.menuUXConfig()

    def menuUXConfig(self):
        customSim = Button(self, text="bruh")
        customSim.place(4,4)


any Idea what to do to fix this?

Thank you

You might have to install the PMW module. I do confess that I don’t know where you might get it, however. (If you happen to be using Ubuntu Linux, I do see that it’s in the Software Centre (the one for 18.04 at least).)

Based on some quick experimentation on my side, what you have seems to be fine.

(Although take that with some salt, as I don’t use PMW/TK myself.)

Note, by the way, that you should be able to replace “base” in that command with “self”, as “base” is a variable that stores the program’s ShowBase instance–which in the case of your program is the program’s “OrbitalWindow” instance.

Thank you so much, I’ve installed the PMW module, and the ModuleNotFound error has gone. However whenever I run it, a fresh error saying:
AttributeError: 'OrbitalWindow' object has no attribute 'tk' appears.
Is there a way to fix that?

Hmm… This is a bit of a guess, but do you have the “want-tk” setting applied, as described further up in the thread?

I don’t have a config.prc file to put want-tk, is that recommended?
I just don’t rly know how to set up a config.prc file

No, you can do it in code, too–there are functions that allow one to set PRC variables in that way.

Specifically, there are two that I’m aware of: “loadPrcFileData” and “loadPrcFile”.

The latter, if I’m not much mistaken, allows one to load a PRC file from code–which is perhaps a bit much for the sake of setting one variable.

The former, however, allows one to load specific variables from code–which would seem to fit this purpose well.

Both, if used, should be imported (and used, I think) before the importation of any other Panda-related modules, I believe.

The “loadPrcFileData” function can be used something like this:

from panda3d.core import loadPrcFileData

loadPrcFileData("", "want-tk #t")

# The rest of your code follows here

Naturally, the same can be done for other PRC variables, too–you can set the window-size, and whether you want PStats, and so on.

Ok so I’ve put that line loadPrcFileData("", "want-tk #t"), however when I do run it, the same error still comes up.

I run that line before any code, including the code that creates the new object OrbitalWindow:

loadPrcFileData("", "want-tk #t")
game = OrbitalWindow()
game.run()

The full error is here in case if it helps (also I am on a Mac running macOS Monterey):

Traceback (most recent call last):
  File "/Users/diyika/PycharmProjects/OrbitalSim/main.py", line 16, in <module>
    game = OrbitalWindow()
  File "/Users/diyika/PycharmProjects/OrbitalSim/orbitalWindow.py", line 23, in __init__
    self.menuUXConfig()
  File "/Users/diyika/PycharmProjects/OrbitalSim/orbitalWindow.py", line 34, in menuUXConfig
    customSim = Button(self, text="bruh")
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 2647, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 2563, in __init__
    BaseWidget._setup(self, master, cnf)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 2532, in _setup
    self.tk = master.tk
AttributeError: 'OrbitalWindow' object has no attribute 'tk'

However thank you so much for your time and help recently, I appreciate it a lot

It’s very much my pleasure. :slight_smile:

But surely above that you have your definition of “OrbitalWindow” and various import-statements? The code that I mentioned should be placed above any Panda-related code–even import-statements!

Something like this:

from panda3d.core import loadPrcFileData
loadPrcFileData("", "win-size 1280 720")

from direct.showbase.ShowBase import ShowBase

class MyGame(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

game = MyGame()
game.run()

Note that the call to “loadPrcFileData” occurs before even the importation of “ShowBase”.

So I’ve put that code at the very top now, with the only line above it being the
from panda3d.core import loadPrcFileData

but the same error appears? I wonder if its because I’m on python 3.9? Since I’m using pycharm it auto installs it into a python 3.9 within my project, however the tk module might only be in the original python 3.9, and not in the new local python 3.9 interpreter PyCharm made.

However when I put the pip install panda3d command into the main terminal on my device, it installs itself into python 3.7. How do I change it so pip installs it in python 3.9?

When I switch interpreters to python 3.7, it recognises modules like pygame3d.core etc…, but when I run it, that same ModuleNotFound Error: no 'Pmw' module appears. The problem is, when I run pip install Pmw, it installs it into python 3.9!

Tl;dr Panda3D is currently on a local python 3.9 version which may not have tk, however when I install directly through cmd it installs into python 3.7 only. The Pmw pip install installs into 3.9.

Is there any way to make panda/pmw install in the same python? As that might be the problem

python3.7 -m pip install Pmw
or
python3.9 -m pip install panda3d