1
from tkinter import *
from tkinter import filedialog


#Functions
def start_game():

    screen = Tk()
    title  = screen.title('Math Duel')
    canvas = Canvas(screen, width=500, height=500)
    canvas.pack()

    #image logo
    logo_img = PhotoImage(file='methbettle.png')
    #resize
    logo_img = logo_img.subsample(2, 2)
    canvas.create_image(250, 150, image=logo_img)


    #Select Path for saving the file
    path_label = Label(screen, text="Single/Multiplayer", font=('Arial', 15))
    select_btn =  Button(screen, text="Launch", bg='red', padx='22', pady='5',font=('Arial', 15))
    #Add to window
    canvas.create_window(250, 280, window=path_label)
    canvas.create_window(250, 330, window=start_game)

# Button to present more





    screen.mainloop()
D.L
  • 4,339
  • 5
  • 22
  • 45
Crimson
  • 11
  • 1

2 Answers2

2

The main problem here is simply that you aren't calling your start_game function.

You need to put this at the left margin of the file, after all of the other code:

start_game()

A more common method is to hide this inside a conditional statement that allows you to run the file directly or import it into a file (see What does if __name__ == "__main__": do?):

if __name__ == '__main__':
    start_game()

Note: your code has a bug that will be exposed when you do this. You also need to change window=start_game to window=select_btn.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • FWIW, the `if __name__ == '__main__'` solution was my original answer as well, but OP appeared to run into some other issues with this approach... – JRiggles Oct 18 '22 at 19:51
0

I think the issue is that you aren't actually starting the application

from tkinter import *
from tkinter import filedialog


# instantiate tk outside of the function
class Screen(Tk):
    def __init__(self):
        super().__init__()  # initialize Tk
        self.title('Math Duel')
        self.start_game()

    def start_game(self):
        self.canvas = Canvas(self, width=500, height=500)
        self.canvas.pack()

        #image logo
        self.logo_img = PhotoImage(file='methbettle.png')
        #resize
        self.logo_img = logo_img.subsample(2, 2)
        self.canvas.create_image(250, 150, image=self.logo_img)

        #Select Path for saving the file
        self.path_label = Label(self, text="Single/Multiplayer", font=('Arial', 15))
        self.select_btn =  Button(self, text="Launch", bg='red', padx='22', pady='5', font=('Arial', 15))
        #Add to window
        self.canvas.create_window(250, 280, window=path_label)
        self.canvas.create_window(250, 330, window=start_game)



if __name__ == '__main__':
    app = Screen()  # instantiate your Screen class
    app.mainloop()  # run the app
JRiggles
  • 4,847
  • 1
  • 12
  • 27
  • Hi, I tried running the code but only got this list of errors: Traceback (most recent call last): File "C:\Users\User\PycharmProjects\MDH\main.py", line 32, in start_game() # call this to begin the GUI 'mainloop' File "C:\Users\User\PycharmProjects\MDH\main.py", line 23, in start_game canvas.create_window(250, 330, window=start_game) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2814, in create_window return self._create('window', args, kw) – Crimson Oct 18 '22 at 17:26
  • File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2776, in _create return self.tk.getint(self.tk.call( _tkinter.TclError: bad window path name "1961173248320start_game" – Crimson Oct 18 '22 at 17:27
  • I've edited my answer to better follow standard OOP practice used in most Tk apps - this should do what you want. – JRiggles Oct 18 '22 at 17:41