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)
Related
So I made a button to copy something to clipboard, but the button itself is always showing, but the text on it only when its pressed, how to fix it?
(Here the part with the button code:)
canvas1 = tk.Canvas(root, width=300, height=300)
canvas1.pack()
def copy_button():
clip = tk.Tk()
clip.withdraw()
clip.clipboard_clear()
clip.clipboard_append(pw)
clip.destroy()
button1 = tk.Button(text="Copy to Clipboard", command=copy_button, bg="grey", fg="white", font=("Helvetica", 12, "bold"))
canvas1.create_window(150, 150, window=button1)
Your disappearing text issue does not appear in Linux OS but I suspect your issue is related to your button1 = tk.Button(....) statement.
The first argument to a tk.Button widget must be its parent (see this link on this requirement) and not a keyword/option.
Try button1 = tk.Button(canvas1, text="Copy to Clipboard", ....). Doing so, you define the tk.Button to be a child of the tk.Canvas widget.
If you are putting more than a button in the canvas, you might want to consider:
defining a tk.Frame to be a child of the tk.Canvas(e.g. frame1=tk.Frame(canvas1)),
replace window=button1 with window=frame1,
letting button1 to be a child of frame1, e.g. button1 = tk.Button(frame1, text="Copy to Clipboard", ....)
Do let me know if this answer addresses your issue.
Other suggestion:
You can remove clip = tk.Tk(), clip.withdraw(), clip.destroy() and replace the clip term in clip.clipboard_clear() and clip.clipboard_append(pw) with root. Here, I assume that you had earlier defined root = tk.Tk() at the start of your code.
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
There have already been several topics on Python/Tkinter, but I did not find an answer in them for the issue described below.
The two Python scripts below are reduced to the bare essentials to keep it simple. The first one is a simple Tkinter window with a button, and the script needs to wait till the button is clicked:
from tkinter import *
windowItem1 = Tk()
windowItem1.title("Item1")
WaitState = IntVar()
def submit():
WaitState.set(1)
print("submitted")
button = Button(windowItem1, text="Submit", command=submit)
button.grid(column=0, row=1)
print("waiting...")
button.wait_variable(WaitState)
print("done waiting.")
windowItem1.mainloop()
This works fine, and we see the printout “done waiting” when the button is clicked.
The second script adds one level: we first have a menu window, and when clicking the select button of the first presented item, we have a new window opening with the same as above. However, when clicking the submit button, I don’t get the “Done waiting”. I’m stuck on the wait_variable.
from tkinter import *
windowMenu = Tk()
windowMenu.title("Menu")
def SelectItem1():
windowItem1 = Tk()
windowItem1.title("Item1")
WaitState = IntVar()
def submit():
WaitState.set(1)
print("submitted")
button = Button(windowItem1, text="Submit", command=submit)
button.grid(column=0, row=1)
print("waiting...")
button.wait_variable(WaitState)
print("done waiting")
lblItem1 = Label(windowMenu, text="Item 1 : ")
lblItem1.grid(column=0, row=0)
btnItem1 = Button(windowMenu, text="Select", command=SelectItem1)
btnItem1.grid(column=1, row=0)
windowMenu.mainloop()
Can you explain it?
Inside your SelectItem1 function, you do windowItem1 = Tk(). You shouldn't use Tk() to initialize multiple windows in your application, the way to think about Tk() is that it creates a specialized tkinter.Toplevel window that is considered to be the main window of your entire application. Creating multiple windows using Tk() means multiple main windows, and each one would need its own mainloop() invokation, which is... yikes.
Try this instead:
windowItem1 = Toplevel()
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 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.