from tkinter import *
parent = Tk()
parent.geometry('500x500')
parent.title('Test in progress...')
parent.attributes('-alpha',0.5)
#parent.attributes('-fullscreen', True)
button1 = Button(parent, text = 'FOUND!',fg='red', command=parent.destroy)
button1.pack()
parent.mainloop()
I want this to flash translucent-red on fullscreen without effecting the user abilty to select things.
Using some help from this answer I think I have something to help you. You need to have a function that will control the changes to color utilizing .after. It shifts from white transparent to red in a "flashing" like manner. I hope this is in the ballpark for what you want.
from tkinter import *
def change_color():
current_color = parent.cget("bg")
next_color = "white" if current_color == "red" else "red"
parent.config(background=next_color)
parent.after(1000, change_color)
parent = Tk()
parent.config(bg="red")
parent.attributes('-alpha', 0.5)
parent.geometry('500x500')
parent.title('Test in progress...')
button1 = Button(parent, text='FOUND!', fg='red', command=parent.destroy)
button1.pack()
change_color()
parent.mainloop()
Related
Hello I am trying to change background by button on tkinter. I have a button that changes background of mainWindow but when button pressed it doesn't change immediately. I think tk.update() command is useless because I made too much test with it and nothing happened.
Here's my code:
import tkinter as tk
def ChangeBg():
global bgColor
bgColor = 'white'
mainWindow.update()
bgColor = 'black'
root = tk.Tk()
mainWindow = tkToplevel()
mainWindow.geometry('500x500')
mainWindow.config(bg=bgColor)
btn = tk.Button(mainWindow, text='change background', command=ChangeBg)
btn.pack()
tk.mainloop()
I guess you will say "Why don't you just use mainWindow.config(bg=bgColor)?". Because in my main code I have to store color in variables and there will be a lot more widgets. Like generalBtnColor, generalForeground, generalTextColor. I can write a lot of codes for update all widgets itself but this is Python and I believe there is a short way.
I added mainWindow.config(bg='white'). Also changed in line 11 and 14.
import tkinter as tk
def ChangeBg():
global bgColor
mainWindow.config(bg='white')
mainWindow.update()
root = tk.Tk()
mainWindow = tk.Toplevel()
mainWindow.geometry('500x500')
mainWindow.config(bg = 'black')
btn = tk.Button(mainWindow, text='change background', command=ChangeBg)
btn.pack()
tk.mainloop()
Output:
Current black:
After changing to white:
I know the code isn't complete but I'm new to coding and Tkinter and couldn't find any good sources around?
from tkinter import*
class textbox:
def __init__(self,parent):
self.s =StringVar
self.s.set(0)
WD=17
f1=frame(parent)
self.frame = frame(f1,parent, width=300, height =300, bg="deeppink")
self.frame.grid(row=1,colimnspan = 3)
self.label(f1,text= "Change Colour", bg ="gray", wraplength=100, width=WD, anchor=CENTER)
self.entry=Entry(f1,width=15, command = self.bg_colour)
self.label.pack(side=LEFT)
self.entry.pack(fill= Y, expand = TRUE)
def bg_colour(self):
self.frame.configure(bg = self.entry)
if __name__=="__main__":
root=Tk()
Frame=textbox(root)
root.mainloop()
I might not be understanding your question correctly, are you trying to change the background color of the textbox right away, or when a button is pressed or an event happens?
I have some code (as shown below) which prompts the user to select which colour to change the GUI to. But my problem is that it only changes the background. I'd like to know if there's a way to change the background of every label and button at once or do I have to change each label/button individually.
import tkinter
window = tkinter.Tk()
colour_frame = tkinter.Frame(window)
options_frame = tkinter.Frame(window)
def colours():
options_frame.pack_forget()
red.pack()
orange.pack()
back_button.pack()
colour_frame.pack()
def back():
options_frame.pack()
colour_frame.pack_forget()
def make_red():
window.configure(background="red")
def make_orange():
window.configure(background="orange")
colour_button = tkinter.Button(options_frame, text="Appearance", command=colours)
red = tkinter.Button(colour_frame, text="RED", command=make_red)
red.configure(bg = "red")
orange = tkinter.Button(colour_frame, text="ORANGE", command=make_orange)
orange.configure(bg = "orange")
back_button = tkinter.Button(colour_frame, text="Back", command=back)
window.mainloop()
You can make a list containing all your widgets you want to change
myWidgets = [button1, label1, ... ] # List of widgets to change colour
for wid in myWidgets:
wid.configure(bg = newColour)
Here's an example code of changing the background colour of multiple labels at once.
import tkinter as tk
# Change all label backgrounds
def change_colour():
c = user.get() #Get the entered text of the Entry widget
for wid in widget_list:
wid.configure(bg = c)
# Create GUI
root = tk.Tk()
tk.Label(root, text='Enter a colour').pack()
user = tk.Entry(root)
user.pack()
label_frame = tk.Frame(root)
label_frame.pack()
btn = tk.Button(root, text='Change Colour', command = change_colour)
btn.pack()
widget_list = [user, btn] # Add defined widgets to list
#Dynamicly create labels for example
for x in range(10):
lbl = tk.Label(label_frame, text='Label '+str(x))
lbl.pack(side = tk.LEFT)
widget_list.append(lbl) #Add widget object to list
root.mainloop()
Or if you have a Frame already containing all the widgets you want to change, then you can use this instead.
parent_widget.winfo_children() will return a list containing all the widgets stored inside the parent widget
def change_colour():
c = user.get()
for wid in label_frame.winfo_children():
wid.configure(bg = c)
Try using ttk for some of your GUI elements. ttk allows you to create styles for widgets and update the style to all widgets at once (at least for those that have the same style). You may need to mix the usage of ttk and tkinter, but it should make things a bit easier in the long run. Here is an example I made:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# Creating a style for the buttons
color_style_button = ttk.Style()
color_style_button.configure("color.TButton", foreground="red")
def change_color(color):
# This function changes the style to all buttons using the "color.Button style"
if color == "red":
color_style_button.configure("color.TButton", foreground="red")
elif color == "blue":
color_style_button.configure("color.TButton", foreground="blue")
elif color == "green":
color_style_button.configure("color.TButton", foreground="green")
frame_a = ttk.Frame(root)
frame_a.pack()
red_button = ttk.Button(frame_a, text="Red", command=lambda: change_color("red"), style="color.TButton")
red_button.pack()
blue_button = ttk.Button(frame_a, text="Blue", command=lambda: change_color("blue"), style="color.TButton")
blue_button.pack()
green_button = ttk.Button(frame_a, text="Blue", command=lambda: change_color("green"), style="color.TButton")
green_button.pack()
root.mainloop()
I recommend checking out this site to learn more about ttk and styles.
So my computing class are making a xmas card in python, and for one of the bits there is going to be a text box with a message, but how do I make the background alternate from green and red ?
If someone would be able to help that would be amazing :)
from tkinter import *
root = Tk()
root.title("Xmas Message")
#command for the button
def test_com():
#removing the button
act_btn.grid_remove()
#adding the textbox for the message
msg_box = Text(root, height = 1, width = 30)
msg_box.grid(row=0, column=0)
#adding the message
msg_box.insert(END, "Happy Xmas")
#changing the background to green
msg_box.config(background="green")
#changing the background to red
msg_box.config(background="red")
root.after(250, test_com)
#button for activating the command
act_btn = Button(root, text = "1", command = test_com)
act_btn.grid(row=0, column=0)
root.mainloop()
Create a change_color callback that alternates the text box's color, and uses after to call itself a second in the future.
Sample implementation:
from tkinter import *
def change_color():
current_color = box.cget("background")
next_color = "green" if current_color == "red" else "red"
box.config(background=next_color)
root.after(1000, change_color)
root = Tk()
box = Text(root, background="green")
box.pack()
change_color()
root.mainloop()
If I created Tkinter window with some text that filled the whole window and now wanted to replace the window with a new text, is there a way to refresh the window?
For Example:
a= 100
win= Tk()
win.geometry("500x300")
while a > 0:
if a%2 == 0:
lbl = Label (win, bg = "purple")
lbl.pack()
else:
lbl = Label (win, bg = "blue")
lbl.pack()
a= x-1
The problem with this code is that the Tkinter window does not refresh and just provides the end result instead of showing the windows changing colors.
Thanks for the help!
That is not the way to change UI states, because even if you refreshed the window it would be so quick you won't notice, instead change the state, wait some time and change the state again e.g. here I show how to animate color
from Tkinter import *
index = 0
def changeColor():
global index
if index%2==0:
label.configure(bg = "purple")
else:
label.configure(bg = "blue")
index+=1
label.after(1000, changeColor)
root = Tk()
mainContainer = Frame(root)
label = Label(mainContainer, text="")
label.configure(text="msg will change every sec")
label.pack(side=LEFT, ipadx=5, ipady=5)
mainContainer.pack()
label.after(1000, changeColor)
root.title("Timed event")
root.mainloop()
This Is How I Do To Update Data From Sql Server in tkinter GUI python3
from tkinter import *
import os
window=Tk()
window.geometry('300x300')
def update():
window.destroy()
os.system('test.py')
Button(window,text="Refresh",command=update)
window.mainloop()