Creating an interconnected menu in Python - python

So I'm teaching myself Python/Pygame, and to start off I'm trying to make a basic menu that the user can scroll through and then select an item to go to a different menu. My initial thought was to define the menus as a tuple of menu items, with each item being a tuple of what text to display and where selecting that item leads you. So, for example:
mainmenu = (("Go to Menu 1",menu1),("Go to Menu 2",menu2))
menu1 = (("Go to Menu 2",menu2),("Return to Main Menu",mainmenu))
menu2 = (("Go to Menu 1",menu1),("Return to Main Menu",mainmenu))
then have a function like makeMenu(menu) that prints out a given menu, and allows the user to scroll through it. When the user makes their selection, makeMenu(menu) is called again, this time being passed the menu that corresponds to the currently selected option.
The problem is that Python won't allow you to define mainmenu in terms of other menus that haven't been defined yet. This makes sense, but I can't think of a way to create a series of interconnected menus without defining them relative to each other. I'm sure there's a simple solution, but for the life of me I can't figure it out. Any thoughts?

Often, for menus, I like to use the State Pattern. A fully fledged one may be overkill for your current needs - but it should lead you in the right direction. An example of this for C, specifically talking about game states, is available on gamedevgeek.com.
This may not directly answer your questions, but it should lead you to a much more extensible and robust solution.

Related

What is the difference between a Primitive, Control and Complex Control widget?

Qt supports custom paint events for drawing of widgets through QStyle.
However when using the QStyle you need to specify what type of element you're trying to draw,
i.e, if you want a control element you need to paint it by using QStyle.drawControl(CE_Example, style).
My question is, how is someone who is relatively new meant to understand which method to correctly call, or find which element to paint when I want to perform the actions myself, say for example I want to draw my SpinButton with purple up and down arrows. I tried to google and see if I could find an explanation but could not, so I was wondering if this is common terminology or if it was some jargon used by the Qt community.
Any advice for correctly traversing documentation or anything like that appreciated, maybe I have an incorrect approach.
I eventually found my answer traversing https://doc.qt.io/qt-5/qstyle.html#details
Primitive Element: A primitive element is a common GUI element, such as a checkbox indicator or button bevel.
Control Element: A control element is a part of a widget that performs some action or displays information to the user.
Complex Control: Complex controls have different behavior depending upon where the user clicks on them or which keys are pressed.

Keeping track of an entry's index in Tkinter menus

I don't have a lot of GUI programming experience and very little in Tkinter, so bear with me. It seems to me that the Tkinter menu interface is seriously wrong. I'm referring in particular to the way menu entries are addressed.
Menu entries are not widgets. add_command - or, for that matter, any add - does not return an object which we can use to address the entry. In order to
reconfigure
delete
insert relative to
an entry, an index must be provided. The index is, usually, just an index in the current list of entries in the menu. That means, all entries that exist (after additions and deletions) at the present time in the index.
It seems logical to organize the code in a GUI around groups of functions that are related from a user's point of view. Say, Undo and Redo should be together in the Edit menu. But the Edit menu may also contain other groups of functions - Cut, Copy, Paste, or perhaps Find. It makes sense to put the code that handles Undo and Redo in one module, the code for Cut, Copy and Paste in another, the code for Find in yet another. These modules are unrelated to each other, except for the fact that they all exist in the Edit menu. However, just because of that fact, it seems that in Tkinter menus they need to know if a module "above" inserts or deletes an entry in its own "area", because that affects the indices of their own entries. Isn't that quite fragile? Or am I missing something?

python gtk toggle combobox?

Is the equivalent of ToggleButton exist in PyGtk ?
I would like to have a callback like: True or False.
self.liste = gtk.ComboBox(self.liststore)
self.liste.connect("changed", self.result_list)
With this method, the program can't detect if the user clicks again on the same choice.
Thanks
I don't have any specific advice as I haven't used ComboBox much, but I'm pretty sure that what you want to do is possible.
The GTK tutorial says
ComboBox uses a TreeModel (usually a ListStore) to provide the list
items to display.
The basic ComboBox methods are great for simple things, but for more advanced usage you need to play with things at the TreeModel level yourself. To do that effectively, you need to know how they work; fortunately the docs in the tutorial are pretty good: TreeView widget, but also check out the info in the reference manual.
To make the equivalent of a toggle button you can use 14.4.8. Activatable Toggle Cells

PyQt Clear the menubar

Is there a quick way to clear the menu bar i.e. Remove all menus in PyQt.
The reason I ask is that I am developing a modular application. Each module provides it's own menus and gives a weighting to where that menu should appear. Each module can also add a menuAction to another modules menu, however when that happens it changes the order of the menus. What i want to do is when the menus have finished being created is to remove them all and then re add them.
I know this will work because I can re add them now and they appear in the correct order but all the previous menus are still there.
did you try QMenu.clear (self)
mymenu = QtGui.QMenu("Menu")
mymenu.clear()

wxpython SearchCtrl two events triggered

I am using a SearchCtrl with a dropdown menu and I'm having some trouble with the events. When I click the little arrow next to the search button, the EVT_SEARCHCTRL_SEARCH_BTN is triggered, which is not what I want. I only want the EVT_MENU_RANGE to be triggered after I clicked an item, and not also the EVT_SEARCHCTRL_SEARCH_BTN before i click it.
self.search_ctrl = wx.SearchCtrl(self.panel_1, -1,
style=wx.TE_PROCESS_ENTER)
self.search_menu = wx.Menu()
self.search_items = {"text1":"value1", "text2":"value2"}
for txt in self.search_items:
self.search_menu.Append(-1, txt)
self.search_ctrl.SetMenu(self.search_menu)
self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, self.search, self.search_ctrl)
self.Bind(wx.EVT_MENU_RANGE, self.onSearchMenu)
Although I should probably add id's to the menu bind, this isn't causing the problem. The code works as expected when I comment out the search button bind.
UPDATE
Apparently this isn't a problem, but a 'feature' of the searchctrl. I tried the wxpython demo and the menu also showed up if I just clicked the search button, and not the arrow. It is apparently one button, instead of the two i thought it was.
Is there a way to accomplish my original request? Do i have to manually modify a textctrl, or is there an other solution?
All the examples I've seen suggest you need to specify a range of IDs when you call your menu bind.
Maybe by default it binds to something unexpected... ?
Edit - In light of your update, it seems likely that you're going to need to make a custom control to me..

Categories

Resources