how to display text after a button is pressed in python Tkinter - python

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

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()

Related

how to display entry widget on new window in tkinter?

from tkinter import *
from tkinter import messagebox
w = Tk()
w.geometry('200x250')
def buttons():
r1 = Tk()
r1.geometry('200x250')
r1.mainloop()
t= Label(text = "MIB",font =("Arial", 49))
t.pack(side = TOP)
e = Label(text = "Email")
e1 =Entry()
e.pack()
e1.pack()
p = Label(text = "Password")
p.pack()
p1 = Entry()
p1.pack()
b= Button(text = "SIGN UP", command = buttons)
b.pack()
w.mainloop()
How I can display the entry widget after clicking submit button in the new window in tkinter python? I tried defining under button function but it is showing me the window but not the widgets. The widgets are only displayed on the first window after clicking submit button.
TK() is the root window, so it needs to be called only once. After that, to open another window, you need to use tkinter.Toplevel(). Below is the code I put labelexample in a new window.
from tkinter import *
from tkinter import messagebox
w = Tk()
w.geometry('200x250')
def buttons():
r1 = Toplevel(w)
r1.geometry('200x250')
labelexample = Label(r1, text = 'GOOD')
labelexample.pack()
t= Label(text = "MIB",font =("Arial", 49))
t.pack(side = TOP)
e = Label(text = "Email")
e1 =Entry()
e.pack()
e1.pack()
p = Label(text = "Password")
p.pack()
p1 = Entry()
p1.pack()
b= Button(text = "SIGN UP", command = buttons)
b.pack()
w.mainloop()

Tkinter pack after destroying

Just a simple example of a problem I experienced:
from tkinter import *
root = Tk()
frame = Frame(root)
label = Label(frame, text = "Hey")
label.pack()
def packframe():
frame.pack()
def destroyframe():
frame.destroy()
pack_button = Button(root, text = "pack", command = packframe)
pack_button.pack()
des_button = Button(root, text = "destroy", command = destroyframe)
des_button.pack()
Once I press the destroy button, I cannot pack it back again on the screen with the pack_button. Im not sure why is it so, but I would appreciate both explanaition and a solution

(Tkinter) Image won't show up in new window

I just started using python tkinter and I have a button that opens a new window. One the new window there is an image, but the image won't show up.Can you please help me solve my problem?
from tkinter import *
def nwindow():
nwin = Toplevel()
nwin.title("New Window")
btn.config(state = 'disable')
photo2 = PhotoImage(file = 'funny.gif')
lbl2 = Label(nwin, image = photo2)
lbl2.pack()
def quit():
nwin.destroy()
btn.config(state = 'normal')
qbtn = Button(nwin, text = 'Quit', command = quit)
qbtn.pack()
main = Tk()
main.title("Main Window")
main.geometry("750x750")
photo = PhotoImage(file = 'funny.gif')
lbl = Label(main, image = photo)
lbl.pack()
btn = Button(main, text = "New Winodw", command = nwindow)
btn.pack()
main.mainloop()
your coding doesn't work but putting .mainloop() should fix your issue
def nwindow():
nwin = Toplevel()
nwin.title("New Window")
btn.config(state = 'disable')
photo2 = PhotoImage(file = 'funny.gif')
lbl2 = Label(nwin, image = photo2)
lbl2.pack()
nwin.mainloop()

Run two Commands when you hit a Tkinter Button

I would like a Tkinter button to clear my current Grid, and also go to a function, and I cannot think of how to do it. I have a grid that is a menu, and in another function I have the code for what was just opened by hitting the button.
in short I want a button, when clicked to do this: self.studyGuide and this: self.frame.grid_forget.
Here is my code:
from tkinter import *
class App:
def __init__(self,master):
frame = Frame(master)
frame.grid()
self.sg = Button(frame, text = "Study Guide", command = self.buttonStart, fg="red")
self.sg.grid(row = 2, column = 1)
self.quizlet = Button(frame, text = "Quizlet", command = self.quizlet)
self.quizlet.grid(row = 2, column = 2)
self.flashcard = Button(frame, text = "Flash Cards", command = self.flashcard)
self.flashcard.grid(row = 2, column = 3)
self.quitButton = Button(frame, text = "Quit", command = frame.quit)
self.quitButton.grid(row = 3, column = 2)
self.text = Label(frame, text = "Social Studies Study Tool")
self.text.grid(row = 1, column = 2)
def buttonStart(frame):
self.studyGuide()
self.frame.grid_forget()
def studyGuide(self):
studyGuide = Frame()
studyGuide.pack()
self.sgText = Label(studyGuide, text = "This is not real.")
self.sgText.pack()
def quizlet(self):
print("Quizlet")
def flashcard(self):
print("Flashcards")
root = Tk()
app = App(root)
root.mainloop()
Simply, have the callback passed to the Button constructor call the other 2 functions:
def foo(self):
self.studyGuide()
self.frame.grid_forget()
root = Tk()
my_button = Button(root, text="I'm doing stuff", command=foo)

New windows in tkinter

I have a bit of difficulty with the code below. Basically, I want the code to, when I press the Enter button, to open the window2 but also close window1 simultaneously so that there is only one window and not two of them.
The code is...
from tkinter import *
def window1():
window = Tk()
window.title("Welcome")
f = Frame()
f.pack()
label1 = Label(window, text = "Welcome to the random window")
label1.pack()
button1 = Button(window, text = "Enter...", command = window2)
button1.pack()
def window2():
screen = Tk()
screen.title("Pop-Up!")
fr = Frame()
fr.pack()
label2 = Label(screen, text = "This is a pop-up screen!")
label2.pack()
button2 = Button(screen, text = "Return", command = window1)
button2.pack()
window1()
This is "Bad" because you're using two instances of Tk. Try instead using TopLevels.
import tkinter as tk
def window1():
window = tk.Toplevel(root)
window.title("Welcome")
# etc etc ...
tk.Button(window,text="Enter...",command=lambda: window2(window)).pack()
def window2(old_window):
old_window.destroy()
# window2 stuff
root = tk.Tk()
root.iconify() # to minimize it, since we're just using Toplevels on top of it
window1()
root.mainloop()
When you are using the Tk() function, you are creating a new instance of the Tcl/tkinter interpreter. Instead use Toplevel() which will make a new window in the current interpreter.

Categories

Resources