Tkinter pack after destroying - python

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

Related

Unable to get entry in tkinter, python

I'm new to coding and I'm trying to grab an input from an entry using tkinter in python. In theory, I should click the 'upload' button, then the code will get the entry and print it for me, but this isn't working. This is my code.
from tkinter import *
root = Tk()
frame = Frame(root)
frame.pack()
def cancel():
quit()
def upload():
Entry.get()
print(Entry)
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
whitebutton = Entry(frame, fg="black")
whitebutton.pack( side = TOP)
redbutton = Button(frame, text="Cancel", fg="red", command = cancel)
redbutton.pack( side = LEFT)
bluebutton = Button(frame, text="Upload URL", fg="blue", command = upload)
bluebutton.pack( side = RIGHT )
root.mainloop()
Does anyone know what's going wrong here?
Thanks, Kieran.
Entry is a class in __init__ file in tkinter folder.
Instead of this:
Entry.get()
print(Entry)
This is what you need
var=whitebutton.get()
print(var)
First of all, it is better to use Tkinter variables rather than normal python variables. Here you need to use StringVar() to set and get an user input from entry. So the complete code -
from tkinter import *
root = Tk()
var = StringVar()
frame = Frame(root)
frame.pack()
def cancel():
quit()
def upload():
print(var.get())
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
whitebutton = Entry(frame,textvariable=var ,fg="black")
whitebutton.pack( side = TOP)
redbutton = Button(frame, text="Cancel", fg="red", command = cancel)
redbutton.pack( side = LEFT)
bluebutton = Button(frame, text="Upload URL", fg="blue", command = upload)
bluebutton.pack( side = RIGHT )
root.mainloop()
For more info - this and this
put root var before root.mainloop()
and make app var and put into "Application(root)"
then change ""root".mainloop()" to "app.mainlop()"
import tkinter as tk
// your code
root = tk.Tk()
app = Application(root)
app.mainloop()

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

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

How do I use a tkinter entry as a parameter for a function

I would like a text box to ask for input in a tkinter window, then use that input as a parameter to call a function that draws a Sierpinski triangle. My buttons work but my input box does not. I keep trying to fix my code but it is not working, any help would be appreciated.
import tkinter as tk
from tkinter import *
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
root.title('Fractals') #titles the button box
top_frame = tk.Frame()
mid_frame = tk.Frame()
prompt_label = tk.Label(top_frame, \
text='Enter a number of iterations (more is better):')
iterations = tk.Entry(root,bd=1)
itr=iterations.get()
itr=int(itr)
button = tk.Button(frame,
text="QUIT",
fg="red",
command=quit)
button.pack(side=tk.LEFT)
sTriangle = tk.Button(frame,
text="Triangle",
command=lambda: sierpinski(fred, (-500,-500), (500,-500),
(0,500),itr))
sTriangle.pack(side=tk.LEFT)
fsquare = tk.Button(frame,
text="Square",
command=fractalsquare(fred,(-500,-500),(500,-500),
(500,500),(-500,500),itr))
fsquare.pack(side=tk.LEFT)
root.mainloop()
There are several issues:
1) Choose one way to import tkinter or confusion will result
2) You should provide a master for your Frames and then pack them. Pay attention on where the frames appear and what they contain.
3) It's usual to assign a textvariable to the Entry which will contain what you enter into it. The textvariable should be a tk.StringVar.
4) If a Button has a callback function, it must be defined before you create the button.
5) The variable fred is not defined.
Example of how you can write it:
import tkinter as tk
root = tk.Tk()
root.title('Fractals') #titles the button box
# Create the Label at the top
top_frame = tk.Frame(root) # Top Frame for
top_frame.pack()
prompt_label = tk.Label(top_frame,
text='Enter a number of iterations (more is better):')
prompt_label.pack()
# Create the Entry in the middle
mid_frame = tk.Frame(root)
mid_frame.pack()
itr_string = tk.StringVar()
iterations = tk.Entry(mid_frame,textvariable=itr_string)
iterations.pack()
fred=None # Was not defined...
# Create Buttons at the bottom
bot_frame = tk.Frame(root)
bot_frame.pack()
button = tk.Button(bot_frame, text="QUIT", fg="red", command=quit)
button.pack(side=tk.LEFT)
def sierpinski(*args): # sTriangle button callback function
itr = int(itr_string.get()) # How to get text from Entry
# if Entry does not contain an integer this will throw an exception
sTriangle = tk.Button(bot_frame, text="Triangle",
command=lambda: sierpinski(fred, (-500,-500), (500,-500),(0,500),itr_string))
sTriangle.pack(side=tk.LEFT)
def fractalsquare(*args): pass # fsquare button callback function
fsquare = tk.Button(bot_frame, text="Square", command=fractalsquare(fred,
(-500,-500),(500,-500),(500,500),(-500,500),itr_string))
fsquare.pack(side=tk.LEFT)
root.mainloop()
You should seriously study a basic tkinter tutorial. Try this one: An Introduction To Tkinter

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

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