-1

I'm currently working on a university assignment. I create a main window with the App class where the game is played. Before this App class is initialized, I want to bring up another window with customized buttons like "BASIC", "ADVANCED", etc. However, currently I can only create a messagebox with YES/NO or YES/NO/CANCEL, etc.

Are there any variations of messagebox that allow customized buttons? From these buttons I want to return a variable gamemode, or something similar, with a value of a string like "BASIC" or "ADVANCED" back to App.__init__().

This is my code so far.

from tkinter import *

class App:
    def __init__(self, master):

        if gamemode = "BASIC"
            'run basic gamemode...'

        elif gamemode = "ADVANCED"
            'run advanced gamemode...'

root = tk.Tk()
app = App(root)
root.mainloop()
Reti43
  • 9,656
  • 3
  • 28
  • 44
Sphero
  • 303
  • 1
  • 3
  • 8
  • 5
    "Is it possible" and questions aren't a good match for stackoverflow, since the only answers are "yes" or "no". In this case, the answer is "yes". Also, "Any help?" is simply too broad. You need to be more specific, and show a bit of research. This isn't a free service for doing your homework. – Bryan Oakley Oct 26 '17 at 15:49

1 Answers1

2

messagebox is used to show messages to the user, a warning, etc, or to providea choice between yes/no/cancel/abort. What you're looking for instead is Radiobutton.

Each Radiobutton has a value and is tied to a variable fetching that value. When you group many such buttons tied to the same variable, you get a one-of-many selections.

Here's a simple example, where the values are integers (starting from 0) for convenience. This is something you can change if you insist on strings. All you need is a frame with your radiobuttons and a launch game button, which reads your choice and passes it to your main app.

As commented by Bryan Oakley, while you can open up a secondary window, you don't need to. You can use the root window to display the choices, and once the user makes the choice you can replace the radiobuttons with the main part of the program. You can display a popup window if you want, but it's not the only solution.

import tkinter as tk

class StartGameMenuWindow:
    def __init__(self, parent):
        self.parent = parent
        self.frame = tk.Frame(parent)
        self.frame.pack()
        self.menu_value = tk.IntVar()
        self._create_items()

    def _create_items(self):
        modes = ('Basic', 'Advanced')
        for value, mode in enumerate(modes):
            tk.Radiobutton(self.frame,
                           text=mode,
                           variable=self.menu_value,
                           value=value).pack()
        tk.Button(self.frame, text='Start game', command=self.launch).pack()

    def launch(self):
        value = self.menu_value.get()
        self.frame.destroy()
        # Launch your game window with `value` as input

root = tk.Tk()
m = StartGameMenuWindow(root)
tk.mainloop()

If you really want a popup window,

# instead of
self.frame = tk.Frame(parent)
self.frame.pack()

# use this, but make sure to rename `self.frame` everywhere in the class
self.window = tk.Toplevel()
Reti43
  • 9,656
  • 3
  • 28
  • 44
  • This is a good answer, though at first glance it doesn't appear to answer what was asked. The OP asks how to put these buttons in a popup window. Your answer is good in that it shows that you don't need to use a separate popup window, but that detail is not spelled out. I recommend you add a paragraph explaining how you can use the same main window by first adding the choices, and then destroying the choices and replacing them with the main window. – Bryan Oakley Oct 27 '17 at 11:53
  • Well, the second sentence in the question (at this time) starts with "I want to bring up another window" – Bryan Oakley Oct 27 '17 at 12:04
  • @BryanOakley It should be addressed now. – Reti43 Oct 27 '17 at 12:16
  • That wasn't what I was suggesting, but it definitely helps tie the answer to the question. – Bryan Oakley Oct 27 '17 at 12:21
  • @BryanOakley Is there possibly a way to reword your suggestion, because I can't see what you meant. My interpretation of the OP's first paragraph is that he wants to create a popup window before the main window is initialised and I believe this is what my answer provides. – Reti43 Oct 27 '17 at 12:28
  • My suggest is to add something like "While you can open up a secondary window, you don't need to. You can use the root window to display the choices, and once the user makes the choice you can replace the radiobuttons with the main part of the program. You can display a popup window if you want', but it's not the only solution". – Bryan Oakley Oct 27 '17 at 12:31