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

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.

Related

How to change background and foreground color of a Tkinter Window on Mac OS X?

I'm trying to set up a GUI window with Tkinter, but somehow I always end up with a blackscreen. Text is (if displayed) always black and so is the background color.
However, when I run tkinter._test() the buttons "Click me!" and "QUIT" are displayed properly.
Is this about background / forground colors, or am I missing something else? This is my code:
import tkinter
gui = tkinter.Tk()
gui.title('Random GUI')
gui.geometry('600x400')
label1 = tkinter.Label(gui, text='Hello World', foreground='black', background='white')
label1.pack()
gui.mainloop()
System information:
macOS Monterey 12.3
Python 3.9.7
Tkinter 8.5.9
There is a mistake in the code:
tkinter takes fg as foreground color and bg as background color
so your code should be something like this :
import tkinter
gui = tkinter.Tk()
gui.title('Random GUI')
gui.geometry('600x400')
label1 = tkinter.Label(gui, text='Hello World', fg='black', bg='white')
label1.pack()
gui.mainloop()
This is how you use MAC OS X.
from tkinter import *
from tkmacosx import Button
root = Tk()
B1 = Button(root, text='Mac Background and Foreground', bg='white',fg='blue', borderless=1)
B1.pack()
root.mainloop()

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

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()

how to lift/raise a tkinter button python

I'm trying to lift a Tkinter button so it overlaps a scrolltext in tkinter, I've tried
button.lift()
button.tag_raise
and I've also tried to make the other widget I want to be under the button lower by using
widget.lower()
widget.tag_lower()
but none of these seem to work for me, it may be because scrolltext is not a fully official tkinter widget and that may cause issues, but if anyone can help please do so! Thank you.
from tkinter import *
from tkinter import font
from tkinter.scrolledtext import ScrolledText
save_button = Button(main, fg="white",
bg="#121212", text="SAVE", font=("Microsoft JHengHei", 10),
border=0, activeforeground="white", activebackground="#141414",
relief=FLAT,
width=12,
command=save_textfile)
save.pack()
scrolltext = scrolledtext.ScrolledText(window, height=11, width=60, bg="#050505", fg="white", bd=0)
scrolltext.pack()
#this should lower the scrolltext
scrolltext.lower()
#and this should lift the save button
save_button.lift()
neither of these work.

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')

python tkinter clicking on button to open new window

I've made 3 buttons on my window. I choosed that the main window should have a specific background image and a full screen.
Now there is a problem. I would like to move to a new window (page) (with an other background and other things) by clicking on button 3.
Things i tryd:
from Main.Info.travelhistry import *
I've added this to the main window to open a new python file with the code of the second screen that has to open when clicking on button 3. But I found out that if I do this both windows will open when running main window.
I added root1 = Tk() at the beginning, root1.mainloop() at the end and between them the code for the other window. But this won't work also, its opening 2 windows like above.
Those were all my attempts and i cant figure out a better way. I can but the background would stay the same. But I have to change the background for the new window to a background image i made...
Any idea what im doing wrong?
from tkinter import *
from tkinter.messagebox import showinfo
from Main.Info.travelhistry import *
def clicked1():
bericht = 'Deze functie is uitgeschakeld.'
showinfo(title='popup', message=bericht)
root = Tk()
a = root.wm_attributes('-fullscreen', 1)
#Hoofdmenu achtergrond
C = Canvas(root, bg="blue", height=250, width=300)
filename = PhotoImage(file = "test1.png")
background_label = Label(root, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
# Geen OV-chipkaart button
b=Button(master=root, command=clicked1)
photo=PhotoImage(file="button1.png")
b.config(image=photo,width="136",height="53", background='black')
b.place(x=310, y=340)
#Buitenland button
b2=Button(master=root, command=clicked1)
photo1=PhotoImage(file="button2.png")
b2.config(image=photo1,width="136",height="53", background='black')
b2.place(x=490, y=340)
#Reis informatie
b3=Button(master=root)
photo2=PhotoImage(file="button3.png")
b3.config(image=photo2,width="136",height="53", background='black')
b3.place(x=680, y=340)
root.mainloop()
root2.mainloop()
You shouldn't call more than one Tk() window.
Instead, tkinter has another widget called Toplevel which can be used to generate a new window.
See below for an example:
from tkinter import *
root = Tk()
def command():
Toplevel(root)
button = Button(root, text="New Window", command=command)
button.pack()
root.mainloop()
This one opens new window that you can edit.
from tkinter import *
Window = Tk()
def Open():
New_Window = Tk()
#You can edit here.
New_Window.mainloop()
Btn1 = Button(text="Open", command=Open)
Bt1n.pack()
Window.mainloop()

Categories