how to handle the "delete window" protocol on messageboxes- Python tkinter - python

Let's say I have a message box in Python tkinter, for example:
from tkinter import messagebox
messagebox.showinfo("Example", "This is an example")
I want to be able to handle the message box's closing protocol. I know you can do it with tkinter windows, like so:
window.protocol("WM_DELETE_WINDOW", on_closing)
But my question is how do you do it with message boxes?

I figured out that what I wanted to do isn't possible (with the help of #Matiiss). So what I did instead was I created a toplevel widget, added buttons to it and handled its closing protocol. Example:
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title("Example")
window.state("zoomed")
def protocol_handler():
# code
window2 = tk.Toplevel(window)
window2.geometry("300x220+500+200")
label1 = tk.Label(window2, text="Would you like to continue?")
label1.place(x=20, y=10)
button1 = tk.Button(window2, text="Yes")
button1.place(x=50, y=100)
button2 = tk.Button(window2, text="No")
button1.place(x=100, y=100)
window2.protocol("WM_DELETE_WINDOW", protocol_handler)
window.mainloop()

Related

How to pop up an existing window by using Tkinter python?

I have created a vocabulary journal by using Tkinter, and I am struggling to pop a window that i created already.
Since i am a beginner, I have not really used class everywhere..
project/build/gui.py
def open_new():# button command
print("clicked")
button_1 = Button(
image=button_image_1,
borderwidth=0,
highlightthickness=0,
command=open_new,
relief="flat"
)
I have created another window in another directory
project/build/build2/gui.py
from db import Database
from pathlib import Path
from tkinter import messagebox
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage,Frame
ob=Database('store.db')
def add_values():
if entry_1.get()=='' or entry_2.get()=='':
messagebox.showerror('no value','please enter anu words')
return
ob.insert(entry_1.get(),entry_2.get())
# print(ob.fetch())
messagebox.showinfo("value has been added", "successfully added")
window = Tk()
blah blah
....
window.mainloop()
WhatI want to do is pop up the second module, which is actually a window. I do not think , it is possible to pop up this window by using toplevel(), is it?
So which code should i use to pop up it?

In Tkinter, How to create a new window when a button is clicked on the previous screen?

In Tkinter, (Python), I want to add a button that creates a new window with some other widgets such as other buttons and Labels. How do I do that?
If I create a button as:
Button(text = "click", command = new_window).pack()
So now what code should I use in the new_window() function in my program?
You can use Toplevel for that.
from tkinter import Tk, Toplevel
from tkinter.ttk import Label, Button
root = Tk()
root.title("Creating multiple windows")
root.geometry("500x500")
def new_window():
top = Toplevel()
top.title("Second window")
top.geometry("400x500") # By default, it is kept as the geometry of the main window, but you can change it.
lab = Label(top, text="This is second window!")
lab.pack(pady=20)
l = Label(root, text="This is the first window")
l.pack(pady=20)
b = Button(root, text="Create new window", command=new_window)
b.pack(pady=50)
root.mainloop()
You can use the “Toplevel” widget for that.
Here’s how you can do that:
from tkinter import *
# NOTE: You can replace the variable names with your own
# Defining a function for a new window to appear
def new_window():
# You can also customize this toplevel with similar attributes
new_toplevel = Toplevel()
# Creating the main GUI
root = Tk()
# You can customize the root window with attributes like .title(), .iconbitmap(), .geometry() etc.
new_button = Button(text=“New Window”, command=new_window)
new_button.pack(pady=10, padx=10) # Paddings are optional and you can also use .grid() with slightly different arguments
root.mainloop()

Trying to write a tkinter popup with dynamic text and confirmation button

I am trying to make a popup for messages such as "Action completed" and I want a button on it to close the popup (A simple OK which quits only the popup). It also sometimes moves behind the window which is annoying. I have some code for the button but it has an issue with the geometry of the shape since the shape isn't exactly defined as it is variable through the size of font and text length.
import tkinter as tk
from tkinter import *
import pygame
import sys
def Text(Text_input, Font, Background, Foreground):
my_window = tk.Tk()
my_window.title(Text_input)
my_window.geometry()
help_label = tk.Label(my_window, font = Font, bg=Background, fg=Foreground, text = Text_input)
help_label.grid(row=0, column=0)
button = tk.Button(my_window, text="QUIT", fg="red", command=quit)
button.pack(side=tk.BOTTOM)
my_window.mainloop()
Text("Hello", "calibri 80", "white", "black")
From my own experience wanting to achieve this, I wrote a simple tkinter code export_success.py as follows:
from tkinter import *
from tkinter import ttk
import sqlite3
from tkinter.ttk import *
root = Tk()
root.geometry('280x100')
root.title("Export Status")
root.config(bg="")
style = ttk.Style()
label_0 = Label(root, text="Export Successful", width=20, background="white", foreground="grey15", font=("Arial, bold", 15)).place(x=50, y=23)
exit1 = Button(root, text='Exit', style='C.TButton', width=11, command=root.destroy).place(x=100, y=60)
root.mainloop()
I then just insert this code os.system('python export_success.py') at the end of my tkinker window that I'm working on.
In this case I'm exporting my information and wanted to know when it is successful.
I am not saying this is best practice and there might be other ways, I just found it to work out for me.

how to make modern button in tkinter

I want to have abutton like this in tkinter (Modern window 10 buttons):
However, I get this button:
The code for my button is:
from tkinter import Tk,Button
root=Tk()
Button(root,text='OK').pack()
Another alternative to create button is to create a label and bind it to the action functions. In the below example .bind() is used to connect the label with respective function. You can design according to your requirements.
from tkinter import *
def OnPressed(event):
print('Hello')
def OnHover(event):
But.config(bg='red', fg='white')
def OnLeave(event):
But.config(bg='white', fg='black')
root = Tk()
But = Label(root, text='Hi', bg='white', relief='groove')
But.place(x=10, y=10, width=100)
But.bind('<Button-1>', OnPressed)
But.bind('<Enter>', OnHover)
But.bind('<Leave>', OnLeave)
root.mainloop()
The themed widgets are in 'themed Tk' aka ttk.
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
ok = ttk.Button(root, text='OK')
ok.pack()
root.mainloop()
Avoid from tkinter import * as both tkinter and tkinter.ttk define Button and many other widgets.
If you use this on Windows you should get something looking like a native button. But this is a theme and can be changed. On Linux or MacOS you will get a button style that is appropriate to that platform.
vol_up = Button(root, text="+",activebackground='orange',bg='yellow')
vol_up.pack(side='top')

How to code for a new window

how can I code for a new window? I have a button that creates a new one but I like to code for it and I don't know how. I think I must define the new window in any way but I don't know how to do this because for opening a new window with help of a button you must define the window self, but no name.
Thanks for helping!
I have create the button and its command in this way:
from Tkinter import *
import Tkinter as tk
master = tk.Tk()
def create_window(): #Definion und Festlegung neues Fenster
toplevel = Toplevel()
toplevel.title('result')
toplevel.geometry('1500x1000')
toplevel.focus_set()
Button(master, text='forward', command=create_window).pack(padx=5, anchor=N, pady=4)
master.mainloop()
Coding for the new window (or creating widgets in the new window) is similar to how you do it in the main window. Just pass the new window (toplevel) as the parent.
Here is an example that creates a Label and an Entry widgets in the new window.
from Tkinter import *
import Tkinter as tk
master = tk.Tk() # Create the main window
def create_window(): #Definion und Festlegung neues Fenster
toplevel = Toplevel()
toplevel.title('result')
toplevel.geometry('1500x1000')
# Create widges in the new window
label = tk.Label(toplevel, text="A Label", fg='blue')
entry = tk.Entry(toplevel)
label.pack()
entry.pack()
toplevel.focus_set()
Button(master, text='forward', command=create_window).pack(padx=5, anchor=N, pady=4)
master.mainloop()

Categories