I am using python 3 I want to close an tkinter window to continue in code, but it doesn't work.
Here is my code:
import tkinter as tk
from tkinter import *
from tkinter import messagebox
def window():
global frame
frame = Tk()
Button1 = tk.Button(frame, text="No.", command=frame.quit)
Button1.pack(anchor=S, fill=X, side=RIGHT)
Button2 = tk.Button(frame, text="Yes!", fg="dark green", command=func)
Button2.pack(anchor=S, fill=X, side=LEFT)
frame.mainloop()
def func():
frame.destroy()
frame.quit()
messagebox.showinfo("Help", "Please help me.")
#Next step ....
window()
I want to exit the script with Button1 and continue and close the window with Button2, but I can't close the window in use of an other function.
import tkinter as tk
from tkinter import messagebox
def window():
global frame
frame = tk.Tk()
Button1 = tk.Button(frame, text="No.", command=func)
Button1.pack(anchor=tk.S, fill=tk.X, side=tk.RIGHT)
Button2 = tk.Button(frame, text="Yes!", fg="dark green", command=func)
Button2.pack(anchor=tk.S, fill=tk.X, side=tk.LEFT)
frame.mainloop()
def func():
global frame
frame.destroy()
messagebox.showinfo("Help", "Please help me.")
#Next step ....
window()
I have solved it,
the problem was: Evry messagebox need an main window, I have destroyed this main window before starting the massagebox and the messagebox started an new emty main window. My resulution is to minimize the main window, start the messagebox and after that to close the massagebox for cotinueing in code.
from tkinter import *
from tkinter import messagebox
main = Tk()
main.geometry("500x400+300+300")
def message():
main.geometry("0x0")
messagebox.showwarning("Say Hello", "Hello World")
main.destroy()
B1 = Button(main, text = "Start Dialog",fg="dark green", command = message)
B1.pack()
main.mainloop()
print("finish dialog")
Related
I have this code and focus dont work with customtkinter
from tkinter import *
import customtkinter as ctk
import sys
if len(sys.argv) > 1:
print("Tk Customized from Tom")
win = ctk.CTk()
win.geometry("750x250")
text = ctk.CTkTextbox(win, width=200, height=200)
text.pack()
text.insert(INSERT, "Hello World!")
entry = ctk.CTkEntry(win, width=100)
entry.pack()
entry.focus_set()
else:
print("Standard Tk ")
win = Tk()
win.geometry("750x250")
text = Text(win, width=30, height=10)
text.insert(INSERT, "Hello World!")
text.pack()
entry = Entry(win, width=30)
entry.pack()
entry.focus_set()
win.mainloop()
py testy.py -> Start standard Tkinter Focus is on Entry
py testy.py 1 -> Start with customtkinter objects Focus is NOT on Entry
How can get focus on CTkEntry ?
An example of what I want to get:
Main program:
from tkinter import *
import tkinter as tk
import module
main_window = Tk()
def createCanvas():
canvas = Canvas(main_window, bg="grey")
canvas.grid(row=1, column=0, columnspan=2)
def resetCanvas():
canvas.destroy()
createCanvas()
button1 = Button(main_window, text="launch module", command=module.moduleFunction)
button1.grid(row=0, column=0)
button2 = Button(main_window, text="reset canvas", command=resetCanvas)
button2.grid(row=0, column=1)
createCanvas()
main_window.mainloop()
Module:
from tkinter import *
import tkinter as tk
from tkinter import Toplevel
def moduleFunction():
child_window = Toplevel()
def resetCanvasFromModule():
# THE QUESTION!
resetCanvas()
button3 = Button(child_window, text="reset canvas from module", command=resetCanvasFromModule)
button3.grid(row=0, column=0)
Obviously this doesn't work, since resetCanvas() isn't defined inside the module, but if I define it, it will try to destroy canvas, which isn't defined in the module, so it won't work either.
So, what would I have to do to get the effect I want?
P.S.: I have tried with 'global', without success.
Can't you just pass the element?
like this
from tkinter import *
canvas = None
def createCanvas():
global canvas
canvas = Canvas(main_window, bg="grey")
canvas.grid(row=1, column=0, columnspan=2)
def ResetCanvasFromModule():
global canvas
canvas.forget()
createCanvas()
button1 = Button(main_window, text="create canvas", command=createCanvas)
button1.grid(row=0, column=0)
button2 = Button(main_window, text="reset canvas", command=ResetCanvas)
button2.grid(row=0, column=1)
Please let me know If this helped you, if not comment so I can edit my answer
Thanks for your help guys.
Passing resetCanvas() as a parameter to moduleFunction(), and a lambda as button1 command, works fine.
This is my working example code(maybe in the future it can be useful to a newbie like me):
Main code:
from tkinter import *
import tkinter as tk
import module
main_window = Tk()
def createCanvas():
global canvas
canvas = Canvas(main_window, bg="grey")
canvas.grid(row=1, column=0, columnspan=2)
return
canvas
def resetCanvas():
canvas.destroy()
createCanvas()
button1 = Button(main_window, text="launch module", command=lambda:[module.moduleFunction(resetCanvas)])
button1.grid(row=0, column=0)
button2 = Button(main_window, text="reset canvas", command=resetCanvas)
button2.grid(row=0, column=1)
createCanvas()
main_window.mainloop()
Module:
from tkinter import *
import tkinter as tk
from tkinter import Toplevel
def moduleFunction(resetCanvas):
child_window = Toplevel()
def resetCanvasFromModule():
resetCanvas()
button3 = Button(child_window, text="reset canvas from module", command=resetCanvasFromModule)
button3.grid(row=0, column=0)
Thanks a lot.
I want to switch the screen using the tkinter module.
Is it possible to switch screens without frame?
This is the code I did.
#frame1.py
from tkinter import *
root=Tk()
root.title('page1')
def goto_p2():
root.destroy()
import frame2
Button(root, text="goto page1", command=goto_p2).pack()
mainloop()
.
#frame2.py
from tkinter import *
root=Tk()
root.title('page1')
def goto_p1():
root.destroy()
import frame1
Button(root, text="goto page2", command=goto_p1).pack()
mainloop()
Here you go, example code how it can be done.
import tkinter as tk
from tkinter import Button,Label
def clear_frame():
for widget in window.winfo_children():
widget.destroy()
def screen_two():
clear_frame()
button_2 = Button(window, text="Go to screen one", command=lambda: screen_one())
button_2.pack(pady=10)
label_2 = Label(window, text="Label on window two")
label_2.pack(pady=10)
def screen_one():
clear_frame()
button_1 = Button(window, text="Go to screen two", command=lambda: screen_two())
button_1.pack(pady=10)
label_1 = Label(window, text="Label on window one")
label_1.pack(pady=10)
window = tk.Tk()
window.title("Test")
window.geometry('250x100')
screen_one()
window.mainloop()
[EDITED]
Function clear_frame() will destroy every widget in frame ( basicly it will clear screen). Besides clear_frame(), there are two more functions, screen_two() and screen_one(). Both of those functions are doing the same thing, calling clear_frame() function, and displaying one button and one label. (Only difference is text inside label and text inside button).
When code runs, it will create window with geometry of 250x100. It will call screen_one() function and run mainloop().
I was doing a project with Tkinter and I ran into a problem. Here is the code:
from tkinter import *
Root = Tk()
def Open():
Root1 = Toplevel()
MyButton = Button(Root, text="Open A New Window!", command=Open).pack()
mainloop()
The problem with this code is that it will open 10 windows if I push the button 10 times. I tried this to solve it:
from tkinter import *
Root= Tk()
def Open():
Root1 = Toplevel()
MyButton = Button(Root, text="Open A New Window!", command = Open, state=DISABLED).grid(row=0, column=0)
MyButton = Button(Root, text="Open A New Window!", command = Open).grid(row= 0, column=0)
mainloop()
But this also doesn't work because after I close the New Window I can't open it again after that because the button will stay disabled.
This below should work. Not the best solution but will allow you to re-enable the button when the second window is closed.
from tkinter import *
Root= Tk()
def Open():
Root1 = Toplevel()
Root1.protocol("WM_DELETE_WINDOW", lambda x=Root1: on_closing(x))
MyButton['state'] = DISABLED
def on_closing(window):
MyButton['state'] = ACTIVE
window.destroy()
MyButton = Button(Root, text="Open A New Window!", command = Open)
MyButton.grid(row= 0, column=0)
mainloop()
So I can do a button in a simple window but not on a Canvas.
from tkinter import *
window = Tk()
def function():
print('Hello World')
tk_button = Button(window, text = 'Click me!', command = function)
I want to position the button on a tkinter Canvas and have graphics around it, not just the bare window with the button.
You can use the create_window function on a canvas to put a frame on it, then use that frame to pack other widgets normally.
import tkinter as tk
mw = tk.Tk()
canvas = tk.Canvas(mw, bg='grey75')
canvas.pack()
frame = tk.Frame(canvas, width=50, height=5)
canvas.create_window((1,1), window=frame, anchor='nw')
button = tk.Button(frame, text='Hello World')
button.pack()
mw.mainloop()