How to make a Tkinter window not resizable? - python

I need a Python script that uses the Tkinter module to create a static (not resizable) window.
I have a pretty simple Tkinter script but I don't want it to be resizable. How do I prevent a Tkinter window from being resizable? I honestly don't know what to do.
This is my script:
from tkinter import *
import ctypes, os
def callback():
active.set(False)
quitButton.destroy()
JustGo = Button(root, text=" Keep Going!", command= lambda: KeepGoing())
JustGo.pack()
JustGo.place(x=150, y=110)
#root.destroy() # Uncomment this to close the window
def sleep():
if not active.get(): return
root.after(1000, sleep)
timeLeft.set(timeLeft.get()-1)
timeOutLabel['text'] = "Time Left: " + str(timeLeft.get()) #Update the label
if timeLeft.get() == 0: #sleep if timeLeft = 0
os.system("Powercfg -H OFF")
os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")
def KeepGoing():
active.set(True)
sleep()
quitButton1 = Button(root, text="do not sleep!", command=callback)
quitButton1.pack()
quitButton1.place(x=150, y=110)
root = Tk()
root.geometry("400x268")
root.title("Alert")
root.configure(background='light blue')
timeLeft = IntVar()
timeLeft.set(10) # Time in seconds until shutdown
active = BooleanVar()
active.set(True) # Something to show us that countdown is still going.
label = Label(root, text="ALERT this device will go to sleep soon!", fg="red")
label.config(font=("Courier", 12))
label.configure(background='light blue')
label.pack()
timeOutLabel = Label(root, text = 'Time left: ' + str(timeLeft.get()), background='light blue') # Label to show how much time we have left.
timeOutLabel.pack()
quitButton = Button(root, text="do not sleep!", command=callback)
quitButton.pack()
quitButton.place(x=150, y=110)
root.after(0, sleep)
root.mainloop()

The resizable method on the root window takes two boolean parameters to describe whether the window is resizable in the X and Y direction. To make it completely fixed in size, set both parameters to False:
root.resizable(False, False)

Related

How do I fix "name is not defined error" (tkinter)

I'm trying to make a stopwatch app with Tkinter, but now I have some trouble. Somewhy, I'm receiving the following error:
name 'label13' is not defined. line 177, in countdown
label13.config(text = count)
I have no idea why this error message pops up. I really appreciate it if you can help me.
Here is my code:
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
from tkinter import *
window = Tk()
window.geometry("703x981")
window.configure(bg = "#FFFFFF")
def countdown(count):
#change text in label
label13.config(text = count)
if count > 0:
#call countdown again after 1000ms (1s)
window.after(1000, countdown, count-1)
countdown(120)
label13 = Label(window, font = "Courier 40 bold", bg="white", fg="black")
label13.place(x =29, y=300)
window.resizable(False, False)
window.mainloop()
Because label13 should defined before calling countdown(120) as shown below.
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
from tkinter import *
window = Tk()
window.geometry("703x981")
window.configure(bg = "#FFFFFF")
window.resizable(False, False)
label13 = Label(window, font = "Courier 40 bold", bg="white", fg="black")
label13.place(x =29, y=300)
def countdown(count):
#change text in label
label13.config(text = count)
if count > 0:
#call countdown again after 1000ms (1s)
window.after(1000, countdown, count-1)
countdown(120)
window.mainloop()

how to change tkinter button for online few seconds

def click():
button1.configure(bg="gray")
time.sleep(1)
button1.configure(bg="green")
button1 = Button(win, text="Button",bg="green",activebackground="red")
button1.pack()
I tryied to change button to gray for only second than change back to green. But it won't change to gray
You need to bind that event after the packing
from tkinter import *
import time
def click(event):
if button1["background"] == "green":
time.sleep(3)
button1["background"] = "yellow"
else:
button1["background"] = "green"
root = Tk()
myContainer1 = Frame(root)
myContainer1.pack()
button1 = Button(myContainer1, text="Button", bg="green")
button1.pack()
button1.bind("<Button-1>", click) # binding event here
root.mainloop()
btw, solid resource on the subject, a bit dated but as an educational material - written perfectly - short :D
http://thinkingtkinter.sourceforge.net/
You have to do it this way.
If you use the Time library, the software won't work
or you can use the Threading module for multithreading in Python but This method is a bit complicated
from tkinter import *
def click(event):
def loop(i):
if i==1:
button1.configure(bg="gray")
elif i==2:
button1.configure(bg="red")
return
i=i+1
button1.after (1000, lambda : loop (i))
loop (1)
root = Tk()
myContainer1 = Frame(root)
myContainer1.pack()
button1 = Button(myContainer1, text="Button", bg="red")
button1.pack()
button1.bind("<Button-1>", click) # binding event here
root.mainloop()
First click() has never been executed. Second all updates are handled by tkinter mainloop(), so those changes will be handled after the function click() exits and the last change will be seen only.
You can use .after() instead of sleep() to change the color of the button back to green after one second:
def click():
button1.configure(bg="gray")
# change the background color back to green after 1 second
button1.after(1000, lambda: button1.configure(bg="green"))
button1 = Button(win, text="Button", bg="green", activebackground="red", command=click)
button1.pack()

after_cancel doesn't work in python tkinter

I made a simple countdown in my trivia game and every time that I go to the next slide it doesn't cancel the previous countdown timer and overlaps with the previous one.
I searched about that on the web and found the after_cancel function that cancels the timer after I go to the next slide and then I recreate it. But it still overlaps even after I added this function.
I think that I didn't give the after_cancel the correct arguments.
from tkinter import *
window = Tk()
window.attributes('-fullscreen',True)
WaitState = StringVar()
count = 10
button_label = StringVar()
def clear():
WaitState.set(1)
for widgets in window.winfo_children():
widgets.destroy()
def clear_time():
clear()
time_up_label = Label(
window,
text="Time is up",
bg = "#6378ff",
fg="#000000",
font=("Arial", 100,"bold"))
time_up_label.place(relx = 0.5,rely = 0.5,anchor = 'center')
continue_but = Button(
window,
text="Continue",
font=("Knewave",25,"bold"),
bg="#942222",
width=11,
height=5,
command=clear)
continue_but.place(relx = 1.0,rely = 1.0,anchor =SE)
continue_but.wait_variable(WaitState)
def button_countdown(i, label):
if i > 0:
i -= 1
label.set(i)
window.after_id = window.after(1000, lambda: button_countdown(i, label))
else:
window.after_cancel(window.after_id)
clear_time()
for index in range(5):
continue_but = Button(
window,
text="Continue",
font=("Knewave",25,"bold"),
bg="#942222",
width=11,
height=5,
command=clear)
continue_but.place(relx = 1.0,rely = 1.0,anchor =SE)
button_label.set(count)
timer_label = Label(
window,
textvariable=button_label,
bg="#6378ff",
fg="#ff0000",
font=("Arial",46,"bold"))
timer_label.pack()
button_countdown(count, button_label)
continue_but.wait_variable(WaitState)
window.mainloop()

Progress bar for loop does not appear until loop calculation is done

I'm having difficulty comprehending how progress bars work in the context of a loop. My example is as follows:
import time
from tkinter import ttk,Tk, Label, Button,Label,DoubleVar
MAX = 4
root = Tk()
root.title("My Progressbar")
root.geometry('500x500')
theLabel = Label(root, text="Progress")
theLabel.pack()
progress_var = DoubleVar()
progress_var.set(0)
progressbar = ttk.Progressbar(root, variable=progress_var,
length=400,maximum=MAX,mode='determinate')
progressbar.pack()
for i in range(MAX+1):
print(i) #Where I will eventually do all my work. Here I print i and pause .5 sec
time.sleep(.5)
progress_var.set(i)
root.update_idletasks()
# Add quit button
def _quit():
root.quit()
root.destroy()
quit_button = Button(master=root, text="Quit", bg='lightgray',command=_quit)
quit_button.pack()
root.mainloop()
I'm missing something obvious as the bar does not appear until after the loop is complete. Also, is it possible to indicate the % complete somewhere on the bar?
You do the "work" before the flow of execution even has a chance to reach root.mainloop. You'll want to simulate doing the work after starting the mainloop. Maybe you could add a button which, upon being clicked, simulates the work? Try this:
# ...
def do_work():
for i in range(MAX+1):
print(i) #Where I will eventually do all my work. Here I print i and pause .5 sec
time.sleep(.5)
progress_var.set(i)
root.update_idletasks()
# ...
do_work_button = Button(master=root, text="Do Work", command=do_work)
do_work_button.pack()
root.mainloop()
If you only just want to increase the value in the progressbar each 0.5 second.Try this:
import time
from tkinter import ttk,Tk, Label, Button,Label,DoubleVar
MAX = 4
root = Tk()
root.title("My Progressbar")
root.geometry('500x500')
theLabel = Label(root, text="Progress")
theLabel.pack()
progress_var = DoubleVar()
progress_var.set(0)
progressbar = ttk.Progressbar(root, variable=progress_var,
length=400,maximum=MAX,mode='determinate')
progressbar.pack()
def add():
if progress_var.get() < MAX:
print(progress_var.get())
progress_var.set(progress_var.get()+1)
root.after(500, add)
# Add quit button
def _quit():
root.quit()
root.destroy()
quit_button = Button(master=root, text="Quit", bg='lightgray',command=_quit)
quit_button.pack()
root.after(500, add)
root.mainloop()
If it is a specific work, you need to create thread or process.

How to make a Label appear then disappear after a certain amount of time in python tkinter

I would like for a cant afford label to appear then after a second disappear when i push a button to buy something. It seems like the time.sleep(1) is making it not work properly. This is done on python tkinter.
def buttonpressed():
Label.place(x = 500, y = 500 #where i want the label to appear
time.sleep(1)
Label.place(x = 10000, y = 10000) #moving it away where i wont be able to see it
You can't use sleep because it stops mainloop
You can use root.after(time_in_milliseconds, function_name) to run function
Example
import tkinter as tk
def button_pressed():
# put text
label['text'] = "Hello World!"
# run clear_label after 2000ms (2s)
root.after(2000, clear_label)
def clear_label():
# remove text
label['text'] = ""
root = tk.Tk()
label = tk.Label(root) # empty label for text
label.pack()
button = tk.Button(root, text="Press Button", command=button_pressed)
button.pack()
root.mainloop()
If you have to create and remove label then use label.destroy()
import tkinter as tk
def button_pressed():
label = tk.Label(root, text="Hello World!")
label.pack()
root.after(2000, destroy_widget, label) # label as argument for destroy_widget
def destroy_widget(widget):
widget.destroy()
root = tk.Tk()
button = tk.Button(root, text="Press Button", command=button_pressed)
button.pack()
root.mainloop()
And shorter version without destroy_widget
import tkinter as tk
def button_pressed():
label = tk.Label(root, text="Hello World!")
label.pack()
root.after(2000, label.destroy)
root = tk.Tk()
button = tk.Button(root, text="Press Button", command=button_pressed)
button.pack()
root.mainloop()
Press button many times to see many labels which disappear after 2s.
You can use after() to set up a callback after a specified interval. In the callback function clear the label with pack_forget() (or grid_forget() if you're using grid). This is better than setting the label's text attribute to an empty string because that causes widgets to be resized, which might not be what you want. Here's an example:
import Tkinter as tk
class App():
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(text='I am a label')
self.label.place(x=0, y=0)
self.label.after(1000, self.clear_label) # 1000ms
self.root.mainloop()
def clear_label(self):
print "clear_label"
self.label.place_forget()
app=App()
Another option is to use self.label.destroy() to destroy the widget, however, pack_forget() allows you to display the label again by calling pack() on the widget again.
# I made it through calling 2 functions:
from tkinter import *
root = Tk()
root.geometry('400x200') # width by height
def one_text():
label['text'] = "Look around"
root.after(1000, another_text)
def another_text():
label['text'] = "and smile"
root.after(1000, one_text)
label= Label(root ,font=('Helvetica', 14), fg='black', bg='white')
label.pack()
one_text()
root.mainloop()

Categories

Resources