Simple GUI using DirectGui

Hi,

I’m trying to do a simple GUI, just for study, but I got a problem that I can’t solve. Suppose that I have a simple menu(DirectScrolledList) with some buttons there, but I want show this menu at right side of the window. I did that, but when I resize window I got some problems, so I tried create a update function, check the size of the window and set it to repositioning the menu to the right side of the window, following the simple rule if pos=(1.8, 0, 0) and my initial window has size x=1000 and y=750, so the new position will be = (1.8*width of the window)/1000, but simple ideas usually fails, and in this situation its not different.

Following my code

from direct.gui.DirectGui import *
from direct.showbase.ShowBase import ShowBase
from panda3d.core import WindowProperties

class Game(ShowBase):
def init(self):
ShowBase.init(self)

    properties = WindowProperties()
    properties.setSize(1000, 750)
    self.win.requestProperties(properties)

    numItemsVisible = 4
    itemHeight = 0.11

    self.myScrolledList = DirectScrolledList(
        decButton_pos=(-0.7, 0, 0.95),
        decButton_text="Up",
        decButton_text_scale=0.04,
        decButton_borderWidth=(0.005, 0.005),
        incButton_pos=(-0.7, 0, -0.95),
        incButton_text="Down",
        pos=(1.8, 0, 0),
        incButton_text_scale=0.04,
        incButton_borderWidth=(0.005, 0.005),
        numItemsVisible=numItemsVisible,
        forceHeight=itemHeight,
        itemFrame_pos=(-0.8, 0, 0.4),
    )

    for cont in range(50):
        b = DirectButton(text=("Button {}".format(cont)),
                         text_scale=0.1, borderWidth=(0.01, 0.01),
                         command=self.click,
                         relief=4)
        self.myScrolledList.addItem(b)

    self.updateTask = taskMgr.add(self.update, "update")

def click(self):
    self.myScrolledList.resetFrameSize()
    print(self.win.properties.size)

def update(self, task):
    x, y = self.win.properties.size
    self.myScrolledList.setPos((x*1.8/1000), 0, 0)
    return task.cont

game = Game()
game.run()

I tried to fin more information about positioning elements in window in documentation, but didn’t find it.
if some one could help me it’ll be appreciated

A simpler solution might be to parent your menu below one or another of the screen-edge anchors provided by ShowBase.

There are several of these, named on the broad pattern of “a2d<Position>”, where <position> is something like “BottomLeft”, etc. More to the point, I believe that these automatically adjust themselves when the window is resized, meaning that they remain at their indication positions–“a2dBottomLeft” is always at the bottom-left of the window, for example.

In your case, I would imagine that one or another of “base.a2dRightCenter”, “base.a2dTopRight”, or “base.a2dBottomRight” might be a good fit, depending on quite where you want it to be anchored.

Once parented there, you should be able to adjust its position somewhat, if desired.

using parent=base.a2dRightCenter solve the problem, thanks!

1 Like

Don’t you think the tkinter module is used for this kind of stuff?

Tkinter can be used–but so can DirectGUI. (Or other GUI systems, I daresay.)

(I know that I don’t use Tkinter, save when using Panda-provided tools that incorporate it.)

Are you aware that you are responding to a solved thread from two months ago?

Tkinter cannot be used for this purpose because it’s used for out-of-game GUI, whereas DirectGUI is an in-game GUI system.