0

Look at this I need to know how to switch between windows easily bceause the method I used did not work at all, i tried destroying the previous canvas and adding a new one between every switch. It worked fine with 2 canvases but when 3 came to play the code just didnt work.


this is the whole code btw.

import tkinter as tk

window = tk.Tk()

canvas1 = tk.Canvas(window, width=400, height=300, bg='gray35', relief='groove', bd=8)
canvas1.pack()

label1 = tk.Label(text='The Guess Game!', font=('Comic Sans MS', 25,'bold'), bg='white', fg='black', bd=8, relief='raised')
canvas1.create_window(200,85, window=label1)

def startGame():
    canvas1.destroy()
    global canvas2
    canvas2 = tk.Canvas(window, width=400, height=300, bg='gray45', relief='groove', bd=8)
    canvas2.pack(side='left')

    btn2 = tk.Button(relief='ridge', bg='green2', width=10, height=5, command=gameEasy)
    canvas2.create_window(100, 150, window=btn2)

    btn3 = tk.Button(relief='ridge', bg='yellow2', width=10, height=5)
    canvas2.create_window(200, 150, window=btn3)

    btn4 = tk.Button(relief='ridge', bg='red2', width=10, height=5)
    canvas2.create_window(300, 150, window=btn4)

    backbtn = tk.Button(width='3', relief='groove', text='x',font=('Calibri', 10,'bold'), bd='8', bg='red', command=goBack)
    canvas2.create_window(30, 30, window=backbtn)

    lbleasy = tk.Label(text='Easy', bg='gray20', fg='white', font=('Impact', 18, 'bold'), bd='8', relief='sunken')
    canvas2.create_window(100, 75, window=lbleasy)

    lblmed = tk.Label(text='Medium', bg='gray20', fg='white', font=('Impact', 18, 'bold'), bd='8', relief='sunken')
    canvas2.create_window(200, 75, window=lblmed)

    lblhard = tk.Label(text='Hard', bg='gray20', fg='white', font=('Impact', 18, 'bold'), bd='8', relief='sunken')
    canvas2.create_window(300, 75, window=lblhard)

    infeasy = tk.Label(text='You have \nan unlimited\namount of\nguesses and the\nsecret number is\nbetween 1-20.', bg='gray45', fg='white', font=('Arial', 10, 'normal'), relief='flat')
    canvas2.create_window(100, 250, window=infeasy)

    infmed = tk.Label(text='You have 25\nguesses and\nthe secret\nnumber is\nbetween 1-50.', bg='gray45', fg='white', font=('Arial', 10, 'normal'), relief='flat')
    canvas2.create_window(200, 250, window=infmed)

    infhard = tk.Label(text='You have 15\n guesses and\nthe secret\n numberis between\n1-100.', bg='gray45', fg='white', font=('Arial', 10, 'normal'), relief='flat')
    canvas2.create_window(300, 250, window=infhard)

def goBack():
    canvas2.destroy()
    global canvas1
    canvas1 = tk.Canvas(window, width=400, height=300, bg='gray35', relief='groove', bd=8)
    canvas1.pack()

    label1 = tk.Label(text='The Guess Game!', font=('Comic Sans MS', 25,'bold'), bg='white', fg='black', bd=8, relief='raised')
    canvas1.create_window(200,85, window=label1)
    
    btn1 = tk.Button(text='Start Game!', command=startGame, font=('Impact', 20, 'bold'), relief='ridge')
    canvas1.create_window(200,180,window=btn1)
    
def gameEasy():
    canvas2.destroy()
    global canvas3
    canvas3 = tk.Canvas(window, width=400, heigth=300, bg='gray25', relief='groove', bd=8)
    canvas3.pack


btn1 = tk.Button(text='Start Game!', command=startGame, font=('Impact', 20, 'bold'), relief='ridge')
canvas1.create_window(200,180,window=btn1)

window.mainloop()
1
  • check this link @NgBANAN Commented Dec 31, 2020 at 3:23

1 Answer 1

0

You misspelled "height" in this line:

 canvas3 = tk.Canvas(window, width=400, heigth=300, bg='gray25', relief='groove', bd=8)
Sign up to request clarification or add additional context in comments.

1 Comment

Well that's embarrassing.. ;D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.