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()
CodePudding user response:
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
.
CodePudding user response:
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