I have quick question about tkinter Python. I created Button added command to execute some function, but how to make, that after clicking button and function execution window would close.
def Top(self):
self.string1=StringVar() ###
self.string2=StringVar()
self.string3=StringVar() ###
self.Top=Toplevel()
self.Top.title("Database Preferences")
L1=Label(self.Top, text="Host")
L1.pack(side=TOP)
self.entry1=Entry(self.Top, textvariable=self.string1)
self.entry1.pack(side=TOP, padx=10, pady=12)
L2=Label(self.Top, text="User")
L2.pack(side=TOP)
self.entry2=Entry(self.Top, textvariable=self.string2)
self.entry2.pack(side=TOP, padx=10, pady=12)
L3=Label(self.Top, text="Pass")
L3.pack(side=TOP)
self.entry3=Entry(self.Top, textvariable=self.string3)
self.entry3.pack(side=TOP, padx=10, pady=12)
Button(self.Top, text="ok", command=self.createini).pack(side=BOTTOM, padx=10, pady=10)
def createini(self):
cfgfile = open("conf.ini",'w')
self.Config = ConfigParser.ConfigParser()
self.Config.add_section('Database')
self.Config.set('Database',"host", self.string1.get())
self.Config.set('Database',"user", self.string2.get())
self.Config.set('Database',"pass", self.string3.get())
self.Config.write(cfgfile)
cfgfile.close()
To destroy the main window you would call the destroy method of that window object. In your case it would be self.Top.destroy() if you want to destroy self.Top.
you can use destroy method which will close your tkinter window
If you want to close the window, you can use the .destroy() method.
Related
How can I manage to automatic cancel this 1st window when I click the next window button?
sample code:
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("GUI practice")
def open():
top = Toplevel() # new window
top.title("Kokomi")
labels = Label(top, text="This one automatically close when i click the next window").pack()
button2 = Button(top,text="Close window", command=top.destroy).pack()
button3 = Button(top,text="Next window", command=open2).pack()
def open2():
top = Toplevel() # new window
top.title("Guide")
labels = Label(top, text="end").pack()
button2 = Button(top, text="Close window", command=top.destroy).pack() # destroy to quit things
button = Button(root, text="Open(No need to close this)", command=open).pack()
root.mainloop()
[Click open][1]
[Click Next window and after that this windows should disappear and continue to the 3rd picture][2]
[The 2nd picture goes disappear when i click the next window][3]
[1]: https://i.stack.imgur.com/plS1T.png
[2]: https://i.stack.imgur.com/EFW76.png
[3]: https://i.stack.imgur.com/xSZCp.png
For this specific case of using two functions and two windows, you can just simply rename the Toplevel widgets to different names and then globalize one and then access it from another function and destroy it before the new windows is shown.
def open():
global top
top = Toplevel() # new window
top.title("Kokomi")
Label(top, text="This one automatically close when i click the next window").pack()
Button(top, text="Close window", command=top.destroy).pack()
Button(top, text="Next window", command=open2).pack()
def open2():
top.destroy() # Destroy previously open window
top1 = Toplevel() # new window
top1.title("Guide")
Label(top1, text="end").pack()
Button(top1, text="Close window", command=top1.destroy).pack() # destroy to quit things
If you noticed, I removed the variable names of buttons and labels because its useless to have those as their value is None, read Tkinter: AttributeError: NoneType object has no attribute <attribute name>.
When you wish to use more functions and windows, you have to manually follow this procedure for all the functions. Unless ofcourse there is a better and cleaner way of designing your app using classes and frames.
Alternatively you can also invoke two functions from a single button, this method will get rid of globalization and renaming and might be a bit better than the above mentioned solution:
def open():
top = Toplevel() # new window
top.title("Kokomi")
Label(top, text="This one automatically close when i click the next window").pack()
Button(top, text="Close window", command=top.destroy).pack()
Button(top, text="Next window", command=lambda: [top.destroy(),open2()]).pack()
def open2():
top = Toplevel() # new window
top.title("Guide")
Label(top, text="end").pack()
Button(top, text="Close window", command=top.destroy).pack() # destroy to quit things
I want to close two windows at the same time when user click the Start button, the new window will pop-up and when the user clicks the Exit button on the Second pop-up window than both the window should Close at a time.
I know that for a different window I have to create a separate function to exit windows But I want to close more than one window with a single click.
I'm using python 3.7!
import tkinter
def NewWindow():
def qExit():
root.destroy()
root = tkinter.Tk()
root.title("New Window")
newButton = tkinter.Button(root, text=" Click here to Exit:",
command=qExit)
newButton.pack()
root.geometry("300x200")
root.mainloop()
Window = tkinter.Tk()
Window.title("hello")
eButton = tkinter.Button(Window, text="Start", command=NewWindow)
eButton.pack()
Window.geometry("200x200")
Window.mainloop()
You shouldn't call tkinter.Tk() more than once in a tkinter application. Call Toplevel() if you want to create a new window.
You also generally don't need to call mainloop() more than once.
To close both the new window and the main window, you can pass the latter to the former when you create it, and then destroy() that in your qExit() function (as well as the new window itself).
Note I changed some of the function and variable names to conform more to the PEP 8 - Style Guide for Python Code guidelines.
import tkinter
def makeWindow(parent):
def qExit():
newWindow.destroy()
parent.destroy()
newWindow = tkinter.Toplevel()
newWindow.geometry("300x200")
newWindow.title("New Window")
newButton = tkinter.Button(newWindow, text=" Click here to Exit",
command=qExit)
newButton.pack()
root = tkinter.Tk()
root.title("hello")
eButton = tkinter.Button(root, text="Start", command=lambda: makeWindow(root))
eButton.pack()
root.geometry("200x200")
root.mainloop()
A simple solution would be to just do exit() to stop the program, which will close all windows. alternatively you can make a list of all open window objects and call destroy on all of them.
No need description
def qExit():
root.destroy()
Window.destroy()
When I make a button that closes the current window and opens another, the current window doesn't close.
from tkinter import *
root = Tk()
def new_window():
root.quit()
new_window = Tk()
new_window.mainloop()
Button(root, text="Create new window", command=new_window).pack()
root.mainloop()
(This isn't my program, it's just an example)
You should be able to do it like this:
import tkinter as tk
root = tk.Tk()
def new_window():
root = tk.Tk()
test = tk.Button(root, text="Create new window", command= lambda:[root.destroy(), new_window()]).pack()
root.mainloop()
test = tk.Button(root, text="Create new window", command= lambda:[root.destroy(), new_window()]).pack()
root.mainloop()
This will literally keep opening the exact same window with a button. The lambda allows you to call multiple functions. By calling .destroy() on your root window, it destroys your window, but doesn’t stop the program. Then you create a new root window with your function.
You can use this technique on your actual script.
I have two queries in this section.
In my code i have created two frames under root, the first frame have "NEXT" button to go on second frame. In second frame there is Run button, which has mapped with close_window function. It should close all windows properly. But in my case not closing it.
When i click "Run" i need to close all windows and need to execute another script on the same directory. Is that possible to do it ?
from Tkinter import *
def close_window():
frame2.destroy()
frame1.destroy()
def swap_frame(frame):
frame.tkraise()
root = Tk()
root.geometry("900x650+220+20")
root.title("Testing")
root.configure(borderwidth="1", relief="sunken", cursor="arrow", background="#dbd8d7", highlightcolor="black")
root.resizable(width=False, height=False)
frame2 = Frame(root, width=900, height=650)
frame1 = Frame(root, width=900, height=650)
Button1 = Button(frame1, text="Next", width=10, height=2, bg="#dbd8d7", command=lambda: swap_frame(frame2))
Button1.place(x=580, y=580)
Button2 = Button(frame2, text="Run", width=10, height=2, bg="#dbd8d7", command=close_window,)
Button2.place(x=580, y=580)
frame2.grid(row=0, column=0)
frame1.grid(row=0, column=0)
root.mainloop()
what is wrong in the code?
"Destroy window not closing all windows properly"
Nor should it. destroy method destroys a widget, and when a widget is destroyed so are its children.
Since neither frame1 nor frame2 are 'windows' or a window's parents, there's no window destruction taking place.
"When I click "Run" I need to close all windows and need to execute another script in the same directory. Is that possible to do it?"
It is possible. Use quit on any GUI object, instead of destroy. It stops the mainloop, hence destroying the entire GUI. In that it solves the first problem as well. Then import another_script:
...
def close_window():
frame1.quit()
...
root.mainloop()
import another_script
I have problem in my Tkinter application.
The code is the following:
def help_stats(self):
self.help_about = tkinter.Toplevel(relief=tkinter.GROOVE)
self.help_about.title('Statistika')
self.help_about.config(width="350", height="300")
self.help_about.resizable(width=tkinter.FALSE, height=tkinter.FALSE)
self.help_about_label = tkinter.Label(self.help_about,
text="Something")
self.help_about_label.pack(side=tkinter.TOP, expand=1,
fill=tkinter.BOTH, padx=20, pady=10)
If I click on something in my menu, this function is called, and a new window is created. I need, from that created window, to delete minimise Button and only leave the closing button. Is that possible?
use attributes as shown below. It removes both minimize and maximize button. I have tested the code on Windows 8.
self.help_about.attributes("-toolwindow",1)
Modify your code as shown below:
def help_stats(self):
self.help_about = tkinter.Toplevel(relief=tkinter.GROOVE)
self.help_about.attributes("-toolwindow",1)
self.help_about.title('Statistika')
self.help_about.config(width="350", height="300")
self.help_about.resizable(width=tkinter.FALSE, height=tkinter.FALSE)
self.help_about_label = tkinter.Label(self.help_about, text="Something")
self.help_about_label.pack(side=tkinter.TOP, expand=1, fill=tkinter.BOTH, padx=20, pady=10)