Python - What is tkinter's default window reference? - python

import tkinter.messagebox
a = tkinter.messagebox.askquestion('','hi')
After the 'askquestion' window closes the tkinter window still remains.
I can resolve this by the following:
import tkinter.messagebox
top = tkinter.Tk()
a = tkinter.messagebox.askquestion('','hi')
top.destroy()
This destroys the window.
My question is:
Is there a way to destroy the window without creating a reference to it?
I tried:
import tkinter.messagebox
a = tkinter.messagebox.askquestion('','hi')
tkinter.Tk().destroy()
but that has no effect.

If you destroy the root window, Tkinter try to recreate one when you call askquestion.
Don't destory the root window. Instead use withdraw.
import tkinter.messagebox
tkinter.Tk().withdraw()
a=tkinter.messagebox.askquestion('','hi')

Related

Multiple tkinter files quit one keep window open

So I have been using Tkinter and I have it set up with a base background. I then open a file that adds my buttons and everything else. I will then have more files for the login and the game so on so on.
My main issue is closing the file. I could just use place.forget but what I really want to do is close the active file. I tried using exit() but all that does is close the window. so in short i want to close the file with the buttons but not the background file.
file_name = background.py
from tkinter import *
from tkinter import messagebox
top=Tk()
top.geometry("700x500")
top.resizable(False,False)
c=Canvas(top,bg="gray16",height=200,width=200)
c.pack()
filename=PhotoImage(file="mountain.png")
background_label=Label(top,image=filename)
background_label.place(x=0,y=0,relwidth=1,relheight=1)
canvas=Canvas(width=400,height=330)
canvas.place(x=150,y=70)
exec(open("./Home_screen.py").read())
top.mainloop()
this basically creates a graphical background and main window
file_name = Home_screen.py
from tkinter import *
from tkinter import messagebox
def rem():
loginPage.place_forget()
loginPage=Button(text="Login", width=20, command=lambda:[rem()], border=2)
loginPage.place(x=271,y=270)
what I would like to do is remove the .place_forget and replace it by closing Home_screen.py

Edit button in Tkinter message-box

I am using Tkinter to make GUI. I included a messagebox in my code, here's the detail:
from tkinter import *
from tkinter.messagebox import *
root=Tk()
askyesno(message="Are you sure you want to choose this text file?")
When the askyesno pops up, there are 2 buttons: Yes and No, but I want to customize these buttons to "OK" and "Cancel", for example. I have been looking for how to customize Tkinter messagebox buttons many times but I found nothing. Any help? Thanks
Use messagebox.askokcancel
from tkinter import Tk,Button,messagebox
root=Tk()
def create_message():
messagebox.askokcancel("Title",'Hello')
b=Button(root,text='Message',command=create_message)
b.pack()
root.mainloop()
You can simply use
from tkinter import *
from tkinter import messagebox
root = Tk()
button=Button("Message",)
messagebox.askokcancel()

Need to show tkinter toplevel window then sleep; happens the other way around

I am trying to show a tkinter Toplevel window, have the script sleep for one second, then continue running. However, the sleep function always occurs before the toplevel window appears. I'm doing this because I want to show an indication to the user that a label text has been copied on click.
I am planning to have the Toplevel show, sleep for one second, then destroy it. However, as it is currently, the sleep occurs first and then the Toplevelshows and is destroyed immediately too fast for the user to perceive.
Is there a way to have these events happen in my desired order? Here is a script demonstrating what I'm dealing with:
from tkinter import *
from tkinter import ttk
import time
root = Tk() # Creating instance of Tk class
def make_new_window(event):
new_window = Toplevel()
clabel = Label(new_window, text = "Copied")
clabel.pack()
time.sleep(1)
l = Label(root, text = "Example text")
l.pack()
l.bind('<1>', make_new_window)
root.mainloop()

Tkinter new window declaration

Recently while using python tkinters declaration has not been working. What i mean by this is that a for any file where tkinter has not been imported before, simple code such as creating a window is not possible. Has anyone else encountered this issue? If so, how is it solved?
Any feedback is appreciated. Thanks.
The code is simply supposed to open a window in tkinter. But, when run, no window is displayed.
from tkinter import *
def sample():
window = Tk()
sample()
That's so interesting.
You have to use tkinter.Tk() instead of Tk().
Thanks
Try This :
from tkinter import * # Importing everything from the Tkinter module
win = Tk() # Initializing Tkinter and making a window
def sample():
new_win = Toplevel() # This creates a new window
sample()
win.mainloop() # If this is not there, the code won't work

How to (re)enable tkinter ttk Scale widget after it has been disabled?

I'm trying to reenable my Scale widget in Python tkinter after disabling it but it doesn't work. I tried multiple options but none are working.
s.state(["normal"]);
s.configure(state='normal');
The error that I'm getting is saying:
_tkinter.TclError: unknown option "-state"
Since you use ttk widget, the state that you needed to reenable your widget is !disabled.
According to ttk states:
A state specification or stateSpec is a list of state names, optionally prefixed with an exclamation point (!) indicating that the bit is off.
try:
import tkinter as tk
import tkinter.ttk as ttk
except ImportError:
import Tkinter as tk
import ttk
root = tk.Tk()
scale = ttk.Scale(root)
scale.pack()
# disable scale
scale.state(['disabled'])
# enable scale
scale.state(['!disabled'])
root.mainloop()

Categories