0

I am trying to display some text after a button is pressed but all I seem to be able to do make it so that text is displayed before the button is pressed or not at all. here is my code so far:

import tkinter

def label1():
    label2 = tkinter.Label(window1, text = "correct")
    label.pack()

def Window2():
    window1 = tkinter.Tk()
    window1.title("start")
    label = tkinter.Label(window1, text= "how do you spell this Sh--ld")
    label.pack()
    points = 0
    i = points + 1
    button = tkinter.Button(window1, text = "ou", command = label1)
    button.pack()



window = tkinter.Tk()
window.title("menu")

button = tkinter.Button(window, text = "start", command = Window2)
button.pack()

I am trying to get the button in the Window2 subroutine to display the text

Oscar Biggins
  • 13
  • 1
  • 8
  • 1
    First of all, you should not use multiple `Tk()` instances. Change `tkinter.Tk()` to `tkinter.Toplevel()` inside `Window2()`. – acw1668 Feb 08 '20 at 16:17

1 Answers1

1

Here is how you can do it

import tkinter

def label1(root):
    label = tkinter.Label(root, text = "correct")
    label.pack()

def Window2():
    window1 = tkinter.Tk()
    window1.title("start")
    label = tkinter.Label(window1, text= "how do you spell this Sh--ld")
    label.pack()
    points = 0
    i = points + 1
    button = tkinter.Button(window1, text = "ou", command = lambda root = window1: label1(root))
    button.pack()



window = tkinter.Tk()
window.title("menu")

button = tkinter.Button(window, text = "start", command = Window2)
button.pack()

window.mainloop()
Mat.C
  • 1,379
  • 2
  • 21
  • 43
  • Can you explain the solution, and why you left the multiple instances of `Tk()`? – AMC Feb 08 '20 at 16:43
  • I don't know where is the problem in lefting multiple tkinter loops running, i'm not an expert in tkinter, he asked for help and i gave it to him , if you think that there is a better solution just write it :) – Mat.C Feb 08 '20 at 16:47
  • _I don't know where is the problem in lefting multiple tkinter loops running, i'm not an expert in tkinter_ Neither am I, I was just sharing advice that seems decently common on here. See, for example: https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged. _if you think that there is a better solution just write it :)_ I disagree, and I'm not a fan of that attitude, I think it's how we end up with ancient questions whose accepted answer is highly upvoted despite being wrong, as the other, far superior ones, remain unseen by the majority of people. – AMC Feb 08 '20 at 17:07