A hoy hoy, I would like to know how I can take a parameter in a function and make it a label and also then .grid_forget() label again (Answer is on a "Restart?" window). I'm making a currency converter from scratch and my code is terrible.
Take a look at one of my Tkinter programs. Look at where it says counter. That is the variable. The Tkinter website is also so helpful! Good luck!
from tkinter import *
import sys
root = Tk()
root.geometry("480x320")
root.title("School Days Left!")
global counter
counter = 30
def upClick():
global counter
counter += 1
def downClick():
global counter
counter -= 1
mButton1.config(text = counter, borderwidth = 0, highlightthickness=0, relief='ridge', pady = "100")
mButton1 = Button(text = counter, command = downClick, height = 4000, width = 320, font = ("Monospace", 200))
mButton1.pack()
root.mainloop()
Related
import tkinter as tk
from tkinter import *
import keyboard
import time
a = 120
root = tk.Tk()
root.state('zoomed')
root.configure(bg='white')
root.resizable(False, False)
a = 30
x = 0
def increase():
global a
a += 10
def ar():
for i in range(50):
labele1.place(x=120, y=a)
root.after(1000, increase)
c1 = Canvas(root, width=700, height=700, bg='gray95')
c1.place(x=450, y=60)
labele1 = tk.Label(c1, text='_______', font='Calibri 20')
startbutton = tk.Button(text='Başla', command=ar)
startbutton.pack()
root.mainloop()
I want to create a moving label which moves per second. Everything looks clear but it doesn't work well. I want to make a car race app but first the walls should move back and it exit the project
The idea to use root.after is correct, but the implementation is wrong. Just because you update the value of a inside a function, tkinter will not automatically update that value with its widget unless you manually ask it to. So what you can do is:
Make the label a canvas object (recommended since you will be making a game and canvas is closer to give you tools you need):
def ar():
global a
a += 10 # Increase the value of a
c1.coords('wall', [120, a]) # Change the coord of the tag 'wall'
root.after(25, ar) # Repeat this function every 25 ms
....
labele1 = tk.Label(c1, text='_______', font='Calibri 20')
c1.create_window(120, a, window=labele1, tags='wall') # Initial coords -> x=120, y=30
Change the location of the label using place_configure:
def ar():
global a
a += 10 # Increase the value of a
labele1.place_configure(y=a)
root.after(25, ar) # Repeat this function every 25 ms
....
labele1 = tk.Label(c1, text='_______', font='Calibri 20')
labele1.place(x=120, y=a)
Plus checking frame size and decreasing functions.
the main problem was a minor bug which you should change in line 37.
import tkinter as tk
from tkinter import *
import keyboard
import time
a = 120
root = tk.Tk()
root.state('zoomed')
root.configure(bg='white')
root.resizable(False, False)
a = 30
x = 0
moving_rate = 10
max_y = 600
min_y = 30
flag_mov_func= 1
def moving_func(): #increase or decrease function
global a
global flag_mov_func
if flag_mov_func :
a += moving_rate
if a + moving_rate == max_y: #simple logic bouncing if it ends
flag_mov_func=0
elif flag_mov_func==0 :
a -= moving_rate
if a - moving_rate == min_y:
flag_mov_func=1
def ar():
moving_func()
labele1.place(x=120, y=a)
root.after(1000, ar)
flag_is_called=1
def button1(): #new update on code to make the button work once.
global flag_is_called
if flag_is_called:
flag_is_called=0
ar()
c1 = Canvas(root, width=700, height=700, bg='gray95')
c1.place(x=450, y=60)
labele1 = tk.Label(c1, text='_______', font='Calibri 20')
startbutton = tk.Button(text='Başla', command=button1)
startbutton.pack()
root.mainloop()
all I need in tkinter help, cos when you click with mouse three times read as once in counter. I know about it. I tried a couple of methods in mouse-event and I try with My current code. Tnx for your help in advance
from tkinter import *
counter=0
def mouse_click(event):
global counter
print('Counter is ',counter)
counter+=1
window = Tk()
window.minsize(300, 100)
label = Label( window, text="Click here")
label.pack()
label.bind( "<Double-Button>", mouse_click)
window.mainloop()
I have found a solution to this code thanks to everyone who tried
from tkinter import *
counter = 0
def mouse_click_times(event):
global counter
print("You press triple button times: ",counter)
counter +=1
windows = Tk()
windows.minsize(400,250)
label = Label(windows, text = "Click me 3x: ",bg = "blue", fg = "white")
label.pack()
label.bind("<Triple-Button-1>",mouse_click_times)
windows["bg"]="green"
exit_from_tkinter_loop = Button(windows, text="Exit from window", bg =
"blue", fg = "white",command=windows.destroy)
exit_from_tkinter_loop.pack(pady = 20)
windows.mainloop()
The event Double-Button is fired for double clicks. If you want to count individual clicks, use Button
from tkinter import *
counter = 0
def mouse_click(event):
global counter
print('Counter is ', counter)
counter += 1
window = Tk()
window.minsize(300, 100)
label = Label(window, text="Click here")
label.pack()
label.bind("<Button>", mouse_click)
window.mainloop()
I'm making some kind of cookie clicker game with Tkinter to practise my Python skills.
This is my code so far:
"""
author: Elxas866
file: BubbleClicker.py
descr.: A cookie clicker-like game
"""
from tkinter import *
from PIL import ImageTk, Image
clicks = 0
def counter():
global clicks
clicks += 1
print(clicks)
def main():
root = Tk()
root.minsize(500, 500) #pixels
root.title("Bubble Cliker")
bubble = Image.open("assets/Bubble.png")
Bubble = ImageTk.PhotoImage(bubble)
image = Button(image=Bubble, command=counter)
image.pack()
image.place(relx=0.5, rely=0.5, anchor=CENTER)
score = Label(text=clicks, font=("Arial", 25))
score.pack()
root.mainloop()
main()#call
Everything works perfectly fine, but it doesn't show my score in the label. It updates it if I print it so technically it works. But in the label it stays 0. Do you now how to fix that?
Thanks in advance!
In the counter() function, after clicks += 1, add score.config(text=score)
So the final function looks like this:
def counter():
global clicks
clicks += 1
score.config(text=score)
Also, just a suggestion: Avoid importing everything from a module.
Final function:
def counter():
global clicks
clicks += 1
score.config(text=clicks)
Correct code:
"""
author: Elxas866
file: BubbleClicker.py
descr.: A cookie clicker-like game
"""
from tkinter import *
from PIL import ImageTk, Image
clicks = 0
def counter():
global clicks
clicks += 1
score.config(text=clicks)
print(clicks)
def main():
root = Tk()
root.minsize(500, 500) #pixels
root.title("Bubble Cliker")
bubble = Image.open("assets/Bubble.png")
Bubble = ImageTk.PhotoImage(bubble)
image = Button(image=Bubble, command=counter)
image.pack()
image.place(relx=0.5, rely=0.5, anchor=CENTER)
global score
score = Label(text=clicks, font=("Arial", 25))
score.pack()
root.mainloop()
main()#call
I want to update my gui every second (i dont actually care about the time, but it should be like realtime). I got a script, where actually a Label as an int should increase by one and displayed. But i don't get the change, also i want to place the Label according to the width of the root, but it doesn't update. Only the update is a problem, maybe someone can help me with that. (Im a Beginner-Noob).
Script:
from tkinter import *
import time
c = 0
root = Tk()
root.title("Real Time Plot")
root.minsize(width = 200, height = 300)
m = root.winfo_reqwidth() / 2
root.update()
Text = Label(root, text = c)
Text.place(x =m, y = 150)
c = c + 1
print(m)
root.mainloop()
I've created a timer so I suppose you can do it like I did. Hope this works for you.
First, create a StringVar:
text = StringVar (value = "text")
Then create a Label with textvariable.
yourlabel = Label(root, textvariable = Text)
And then just make a function that will update the yourlabel:
yourlabel.set(and here you pass the new value)
So then your yourlabel will have the value that you ve passed. So then you can use something like self.after to update a func every 1000 ms.
You have to use a Python Threading, which will carry out this flawlessly!
Here is the code:
from tkinter import *
import threading
import time
def realtime_update():
while True:
global c, Text
c += 1
Text.configure(text=c+1)
time.sleep(0.001)
root_width = root.winfo_geometry()
root_width = root_width.split('x')
root_width = int(root_width[0])
Text.place(x=root_width/2, y=150)
time.sleep(0.01)
c = 0
root = Tk()
root.title("Real Time Plot")
root.minsize(width = 200, height = 300)
m = root.winfo_reqwidth() / 2
Text = Label(root, text = c)
Text.place(x=int(m), y = 150)
print(m)
threading.Thread(target=realtime_update, daemon=True).start()
root.mainloop()
It solves the following:
Realtime Update (precision to 0.01s)
Label changes position when root window resizes
You are updating just the variable value, not the value of the label.
You need to update the label with the content of c after increasing it:
Text.configure(text=c)
So it becomes something like that:
from tkinter import *
import time
c = 0
root = Tk()
root.title("Real Time Plot")
root.minsize(width = 200, height = 300)
m = root.winfo_reqwidth() / 2
root.update()
Text = Label(root, text = c)
Text.place(x =m, y = 150)
root.update()
time.sleep(2)
c = c + 1
Text.configure(text=c)
root.update()
print(m)
root.mainloop()
I did some minor update to your code, like adding a sleep time, to better show the behaviour.
I am writing a simple program that pulls up an image (BackgroundFinal.png) and displays it in a window. I want to be able to press a button on the window to move the picture down by 22 pixels. Everything works except the button does not do anything.
import Tkinter
import Image, ImageTk
from Tkinter import Button
a = 0 #sets inital global 'a' and 'b' values
b = 0
def movedown(): #changes global 'b' value (adding 22)
globals()[b] = 22
return
def window(): #creates a window
window = Tkinter.Tk();
window.geometry('704x528+100+100');
image = Image.open('BackgroundFinal.png'); #gets image (also changes image size)
image = image.resize((704, 528));
imageFinal = ImageTk.PhotoImage(image);
label = Tkinter.Label(window, image = imageFinal); #creates label for image on window
label.pack();
label.place(x = a, y = b); #sets location of label/image using variables 'a' and 'b'
buttonup = Button(window, text = 'down', width = 5, command = movedown()); #creates button which is runs movedown()
buttonup.pack(side='bottom', padx = 5, pady = 5);
window.mainloop();
window()
If I am not mistaken, the button should change the global 'b' value, therefore changing the y position of the label. I really appreciate any help, sorry for my god-awful conventions. Thanks in advance!
You have a few problems here.
First, you're using pack and place. In general, you should only use 1 geometry manager within a container widget. I don't recommend using place. That's just too much work that you need to manage.
Second, you're calling the callback movedown when you construct your button. That's not what you want to do -- You want to pass the function, not the result of the function:
buttonup = Button(window, text = 'down', width = 5, command = movedown)
Third, globals returns a dictionary of the current namespace -- It's not likely to have an integer key in it. To get the reference to the object referenced by b, you'd need globals()["b"]. Even if it did, changing the value of b in the global namespace won't change the position of your label because the label has no way of knowing that change. And in general, if you need to use globals, you probably need to rethink your design.
Here's a simple example of how I would do it...
import Tkinter as tk
def window(root):
buf_frame = tk.Frame(root,height=0)
buf_frame.pack(side='top')
label = tk.Label(root,text="Hello World")
label.pack(side='top')
def movedown():
buf_frame.config(height=buf_frame['height']+22)
button = tk.Button(root,text='Push',command=movedown)
button.pack(side='top')
root = tk.Tk()
window(root)
root.mainloop()
Thanks for the reply but, It was not really what I was looking for. I'll post what I found worked best here for anybody else with the same problem.
Essentially, It is much better, in this case, to use a Canvas instead of a label. With canvases, you can move objects with canvas.move, here is a simple example program
# Python 2
from Tkinter import *
# For Python 3 use:
#from tkinter import *
root = Tk()
root.geometry('500x500+100+100')
image1 = PhotoImage(file = 'Image.gif')
canvas = Canvas(root, width = 500, height = 400, bg = 'white')
canvas.pack()
imageFinal = canvas.create_image(300, 300, image = image1)
def move():
canvas.move(imageFinal, 0, 22)
canvas.update()
button = Button(text = 'move', height = 3, width = 10, command = move)
button.pack(side = 'bottom', padx = 5, pady = 5)
root.mainloop()
my code may not be perfect (sorry!) but that is the basic idea. Hope I help anybody else with this problem