0

im making a small game in a canvas with turtle and a menu with tkinter. the game stays in the tkinter window. so far im able to manipulate the canvas when pressing on a button, and to manipulate the tkinter window. but after pressing on the game button i want the 2 buttons to disappear. to hide or to be deleted. i cannot figure out how to do that? could anybody help me with this? this is my code:

import tkinter as tk
from turtle import RawTurtle

root = tk.Tk()
root.geometry("200x200")

canvas = tk.Canvas(master = root, width = 0, height = 0)
canvas.pack()

def testdel():   
    global canvas
    canvas.config(width = 0, height = 0)
    root.geometry("200x200")

def testteken():
    global canvas
    root.geometry("1000x1000")
    canvas.config(width = 100, height = 100)
    t = RawTurtle(canvas)
    t.pencolor("#ff0000") # Red
    t.goto(200, 110)
    t.pendown()
    t.goto(0, 0)
    t.penup()

button1 = tk.Button(master = root, text = "delete", command = testdel).pack(side = tk.LEFT)
button2 = tk.Button(master = root, text = "draw", command = testteken).pack(side = tk.LEFT)

root.mainloop()

update:

i fixed this problem by changing the 'pack' code behind the button to the normal pack-way >> button1.pack() after this theo's solution did work :)

Wwes
  • 75
  • 9

1 Answers1

2

To hide a widget temporarily, use pack_forget().

e.g.

def delete_buttons():
    button1.pack_forget()
    button2.pack_forget()

and use command=delete_buttons in the game button.

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
theo
  • 56
  • 6
  • hi theo, i have tried that. the following error comes upw ith that code: return self.func(*args) File "C:\HBO ICT blok 1\bridging course\casus\python oefen\turtle\2 - kopie.py", line 27, in delete_buttons button1.pack_forget() AttributeError: 'NoneType' object has no attribute 'pack_forget' – Wwes Oct 17 '20 at 17:03
  • update: i think i fixed it: i changed the pack code to a new line and button1.pack() after this the pack_forget did work :) – Wwes Oct 17 '20 at 17:10
  • @Wwes You may intrested in https://stackoverflow.com/questions/63079633/tkinter-grid-forget-is-clearing-the-frame/63079747#63079747 – Thingamabobs Oct 17 '20 at 19:35