1

I have created two files, one with Tkinter code and another with functions that generate different mazes using pyamaze. I imported the functions file to my Tkinter code. When a maze is made using pyamaze, it automatically opens its own window. I assumed that it would continue to open its own window if I just added the generate maze function to a Tkinter button. Instead, a blank window titled "PYTHON MAZE WORLD by Learning Orbis" is displayed along with the maze in the Tkinter window rather than a separate window for the maze. I have attached both codes below (I left out any irrelevant lines of code):

Tkinter code

import tkinter as tk
from tkinter import messagebox
import sqlite3
import re
from functions import *

# Constants (written in caps)
REGULAR_FONT = ('Georgia', 14)
UNDERLINED_FONT = ('Georgia', 14, 'underline')
BIG_UNDERLINED_FONT = ('Georgia', 16, 'underline')
ITALIC_FONT = ('Georgia', 14, 'italic')
BOLD_FONT = ('Georgia', 14, 'bold')
SMALL_FONT = ('Georgia', 12)
WINDOW_WIDTH, WINDOW_HEIGHT = 800, 800

# define numbers variable
numbers = set('0123456789')

# define logged username as global variable (for HomePage)
logged_user = ''

class MainDisplay(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        # fill space allocated and expand if needed
        container.pack(side='top', fill='both', expand=True)

        # set minimum to 0 and priority in container
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        # create dictionary for frames
        self.frames = {}

        # puts frames (pages) into dictionary
        for F in (LoginPage, RegistrationForm, ChangePasswordForm, HomePage, LevelSelection, Freeplay):
            # put in container
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky='nsew')

        # display Login Page
        self.show(LoginPage)

    # create function which shows the chosen frame (page)
    def show(self, controller):
        frame = self.frames[controller]
        frame.tkraise()

class HomePage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        tk.Frame.config(self, background='#E1D4E7')
        homepage = tk.Frame(self, bd=1, background='#FFFFFF', relief='solid', padx=20, pady=20)

        # display username, gamemode options and explanations
        tk.Label(homepage, text=logged_user, background='#FFFFFF', font=REGULAR_FONT).grid(row=0, column=0,
                                                                                           columnspan=2, pady=10)
        tk.Label(homepage, text='Choose a gamemode', background='#FFFFFF', font=BIG_UNDERLINED_FONT).grid(
            row=1, column=0, columnspan=2, pady=10)

        tk.Label(homepage,
                 text='>  There are 20 different levels where mazes \n '
                      'get progressively more difficult \n '
                      ' \n '
                      '>  A maximum of 3 stars are awarded based \n '
                      'on the time taken to complete the maze \n'
                      ' \n '
                      '>  The faster the maze is completed, \n '
                      'the more stars will be awarded \n '
                      ' \n '
                      '>  Stars must be collected to move on to the next level \n ',
                 background='#FFFFFF', font=REGULAR_FONT).grid(row=3, column=0, pady=10)
        tk.Label(homepage,
                 text='>  There are 3 levels of difficulty to \n '
                      'choose from: Easy, Medium and Hard \n '
                      ' \n '
                      '>  An unlimited number of mazes can be played \n '
                      'with time scores (time taken) recorded \n',
                 background='#FFFFFF', font=REGULAR_FONT).grid(row=3, column=1, pady=10)

        # Levels and Freeplay Buttons (Redirect)
        levels_button = tk.Button(homepage, width=10, text='Levels', font=ITALIC_FONT,
                                  command=lambda: controller.show(LevelSelection))
        freeplay_button = tk.Button(homepage, width=10, text='Freeplay', font=ITALIC_FONT,
                                    command=lambda: controller.show(Freeplay))
        levels_button.grid(row=2, column=0, pady=10)
        freeplay_button.grid(row=2, column=1, pady=10)

        # Back to Login Page Button
        back_loginpage = tk.Button(homepage, width=15, text='Back to Login Page', font=BOLD_FONT,
                                   command=lambda: controller.show(LoginPage))
        back_loginpage.grid(row=4, column=0, columnspan=2, pady=10)

        homepage.place(anchor='center', relx=.5, rely=.5)

class LevelSelection(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        tk.Frame.config(self, background='#E1D4E7')
        level_selection = tk.Frame(self, bd=1, background='#FFFFFF', relief='solid', padx=20, pady=20)

        # Level Buttons (Redirect)
        level1 = tk.Button(level_selection, width=5, height=5, text='1', font=BOLD_FONT, command=lambda: one())
        level2 = tk.Button(level_selection, width=5, height=5, text='2', font=BOLD_FONT, command=lambda: two())
        level3 = tk.Button(level_selection, width=5, height=5, text='3', font=BOLD_FONT, command=lambda: three())
        level4 = tk.Button(level_selection, width=5, height=5, text='4', font=BOLD_FONT, command=lambda: four())
        level5 = tk.Button(level_selection, width=5, height=5, text='5', font=BOLD_FONT, command=lambda: five())
        level6 = tk.Button(level_selection, width=5, height=5, text='6', font=BOLD_FONT, command=lambda: six())
        level7 = tk.Button(level_selection, width=5, height=5, text='7', font=BOLD_FONT, command=lambda: seven())
        level8 = tk.Button(level_selection, width=5, height=5, text='8', font=BOLD_FONT, command=lambda: eight())
        level9 = tk.Button(level_selection, width=5, height=5, text='9', font=BOLD_FONT, command=lambda: nine())
        level10 = tk.Button(level_selection, width=5, height=5, text='10', font=BOLD_FONT, command=lambda: ten())
        level1.grid(row=0, column=0, padx=5, pady=5)
        level2.grid(row=0, column=1, padx=5, pady=5)
        level3.grid(row=0, column=2, padx=5, pady=5)
        level4.grid(row=0, column=3, padx=5, pady=5)
        level5.grid(row=1, column=0, padx=5, pady=5)
        level6.grid(row=1, column=1, padx=5, pady=5)
        level7.grid(row=1, column=2, padx=5, pady=5)
        level8.grid(row=1, column=3, padx=5, pady=5)
        level9.grid(row=2, column=0, padx=5, pady=5)
        level10.grid(row=2, column=1, padx=5, pady=5)

        level_selection.place(anchor='center', relx=.5, rely=.5)

class Freeplay(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        tk.Frame.config(self, background='#E1D4E7')
        freeplay = tk.Frame(self, bd=1, background='#FFFFFF', relief='solid', padx=20, pady=20)

        Level_easy = tk.Button(freeplay, width=5, height=5, text='1', font=BOLD_FONT, command=lambda: easy())
        level_medium = tk.Button(freeplay, width=5, height=5, text='2', font=BOLD_FONT, command=lambda: medium())
        level_hard = tk.Button(freeplay, width=5, height=5, text='3', font=BOLD_FONT, command=lambda: hard())

        freeplay.place(anchor='center', relx=.5, rely=.5)

if __name__ == "__main__":
    window = MainDisplay()
    window.title('Maze Game')

    # Display window at the center of the screen
    screen_width = window.winfo_width()
    screen_height = window.winfo_height()

    window_x = (screen_width / 2) - (WINDOW_WIDTH / 2)
    window_y = (screen_height / 2) - (WINDOW_HEIGHT / 2)
    window.geometry(f'{WINDOW_WIDTH}x{WINDOW_HEIGHT}+{int(window_x)}+{int(window_y)}')

    window.mainloop()

pyamaze code

from pyamaze import maze, agent, COLOR
from queue import PriorityQueue
import tkinter as tk

# for levels gamemode:
# create functions which generates mazes for each level
def one():
    level_1 = maze(10, 10)
    # loop percentage is increased to create more pathways between two points
    level_1.CreateMaze(loopPercent=50, theme=COLOR.light)
    player = agent(level_1, shape='arrow', footprints='True', color=COLOR.blue)
    level_1.enableArrowKey(player)
    level_1.run()

def two():
    level_2 = maze(10, 10)
    level_2.CreateMaze(loopPercent=40, theme=COLOR.light)
    player = agent(level_2, shape='arrow', footprints='True', color=COLOR.blue)
    level_2.enableArrowKey(player)
    level_2.run()

def three():
    level_3 = maze(10, 10)
    level_3.CreateMaze(loopPercent=30, theme=COLOR.light)
    player = agent(level_3, shape='arrow', footprints='True', color=COLOR.blue)
    level_3.enableArrowKey(player)
    level_3.run()

def four():
    level_4 = maze(10, 10)
    level_4.CreateMaze(loopPercent=20, theme=COLOR.light)
    player = agent(level_4, shape='arrow', footprints='True', color=COLOR.blue)
    level_4.enableArrowKey(player)
    level_4.run()

def five():
    level_5 = maze(10, 10)
    level_5.CreateMaze(theme=COLOR.light)
    player = agent(level_5, shape='arrow', footprints='True', color=COLOR.blue)
    level_5.enableArrowKey(player)
    level_5.run()

def six():
    level_6 = maze(12, 12)
    level_6.CreateMaze(theme=COLOR.light)
    player = agent(level_6, shape='arrow', footprints='True', color=COLOR.blue)
    level_6.enableArrowKey(player)
    level_6.run()

def seven():
    level_7 = maze(14, 14)
    level_7.CreateMaze(theme=COLOR.light)
    player = agent(level_7, shape='arrow', footprints='True', color=COLOR.blue)
    level_7.enableArrowKey(player)
    level_7.run()

def eight():
    level_8 = maze(16, 16)
    level_8.CreateMaze(theme=COLOR.light)
    player = agent(level_8, shape='arrow', footprints='True', color=COLOR.blue)
    level_8.enableArrowKey(player)
    level_8.run()

def nine():
    level_9 = maze(18, 18)
    level_9.CreateMaze(theme=COLOR.light)
    player = agent(level_9, shape='arrow', footprints='True', color=COLOR.blue)
    level_9.enableArrowKey(player)
    level_9.run()

def ten():
    level_10 = maze(20, 20)
    level_10.CreateMaze(theme=COLOR.light)
    player = agent(level_10, shape='arrow', footprints='True', color=COLOR.blue)
    level_10.enableArrowKey(player)
    level_10.run()

# for freeplay gamemode:
# create functions which generates mazes for each difficulty level
def easy():
    level_easy = maze(20, 20)
    level_easy.CreateMaze(theme=COLOR.light)
    player = agent(level_easy, shape='arrow', footprints='True', color=COLOR.blue)
    level_easy.enableArrowKey(player)
    level_easy.run()

def medium():
    level_medium = maze(20, 20)
    level_medium.CreateMaze(theme=COLOR.light)
    player = agent(level_medium, shape='arrow', footprints='True', color=COLOR.blue)
    level_medium.enableArrowKey(player)
    level_medium.run()

def hard():
    level_hard = maze(20, 20)
    level_hard.CreateMaze(theme=COLOR.light)
    player = agent(level_hard, shape='arrow', footprints='True', color=COLOR.blue)
    level_hard.enableArrowKey(player)
    level_hard.run()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ari
  • 11
  • 3
  • Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/q/48045401/16775594). If you look in the [pyamaze source code](https://github.com/MAN1986/pyamaze/blob/main/pyamaze/pyamaze.py), you can see [`self._win=Tk()`](https://github.com/MAN1986/pyamaze/blob/539b23f804a8bc07c89550e3d28e05ed343cf17f/pyamaze/pyamaze.py#L621). So `pyamaze` is creating a second instance of `Tk`. You can either submit a pull request to `pyamaze`, or copy its source code and change it, in accordance with the [license](https://github.com/MAN1986/pyamaze/blob/main/LICENSE), to try to fix this. – Sylvester Kruin Feb 28 '22 at 21:22

0 Answers0