I am trying to create a tkinter application where every time you press a button an image moves down a certain number of pixels. For example, the first time it is placed at y=30 and then the next time the button is pressed it is placed at y=60 etc. Is there any way to do this? I do not want to use the pack() method as I need to place the image in a specific location on the screen using x and y coordinates.
import calendar
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('800x800')
def display():
box_image = tk.PhotoImage(file='apple.png')
panel2 = tk.Label(root, image=box_image, bg='#f7f6f6')
panel2.image = box_image
panel2.place(x=30, y=30 + 30) #i was thinking about doing something like adding 30 each time but this didn't work
button = tk.Button(root, text="click me", command=display)
button.place(x=0, y=0)
root.mainloop()
do the following :
import calendar
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('800x800')
x = 30
y = 30
box_image = tk.PhotoImage(file=r'apple.png')
def display():
global x , y
panel2 = tk.Label(root, image=box_image, bg='#f7f6f6')
panel2.place(x=x+30, y=y+30)
x = x+30
y = y+30
button = tk.Button(root, text="click me", command=display)
button.place(x=0, y=0)
root.mainloop()
Related
What my code does currently is create a label with the text "Hello" every time I press the button that says "Say hello"
What I'm trying to figure out is how to create a button that clears off all of the labels off the screen, however I'm completely clueless.
How do I create a button that clears all of the labels off of the screen?
My code is down below.
import tkinter as tk
import time
root = tk.Tk()
root.geometry("700x500")
h = "Hello"
def CreateLabel():
helloLabel = tk.Label(root, text=h)
helloLabel.pack()
labelButton = tk.Button(root, text="Say hello", command=CreateLabel)
labelButton.pack()
root.mainloop()
I'm beginner but try this:
import tkinter as tk
import time
root = tk.Tk()
root.geometry("700x500")
h = "Hello"
liste = []
def CreateLabel():
helloLabel = tk.Label(root, text=h)
helloLabel.pack()
liste.append(helloLabel)
def DelLabel():
for i in range(len(liste)):
liste[i].destroy()
liste.clear()
labelButton = tk.Button(root, text="Say hello", command=CreateLabel)
labelButton.pack()
labelButton = tk.Button(root, text="del hello", command=DelLabel)
labelButton.pack()
root.mainloop()
I would like the buttons to be placed in random points of the root. I do not understand why they all stack in the middle, since I think I am placing them at random x and y
import tkinter as tk
from tkinter import *
from random import randint
num_buttons = int(input("How many buttons do you want?"))
buttons = [0] * num_buttons
root = tk.Tk()
canvas = tk.Canvas(root, height=700, width=700, bg="dark slate grey")
canvas.pack()
frame = tk.Frame(root, bg="red")
frame.place(relheight=0.6, relwidth=0.6, rely=0.2, relx=0.2)
for k in range(num_buttons):
randx = int(randint(-100,100))
randy = int(randint(-100,100))
buttons[k] = tk.Button(frame, text="DO NOT CLICK ME",
fg="Black", highlightbackground="orange",
command=you_clicked)
buttons[k].place(x=randx, y=randy)
buttons[k].pack()
root.mainloop()
You should not be calling both place and pack. Only one geometry manager can control a widget. When you call pack, any effect a previous call to place is thrown away.
When I click a button, it should write text under it and the text should disappear after few seconds.
I don't know how to code that. What I have tried so far:
from tkinter import *
import time
window = Tk()
window.title("Button")
window.geometry("500x300")
def buttonclick():
tex = Label(text="You clicked the button")
tex.pack()
time.sleep(5)
tex.destroy()
but = Button(text="Click me!", command=buttonclick)
but.pack()
window.mainloop()
You can use .after() method to destroy the label after fixed period of time.
The following example will delete the label after 3 seconds:
from tkinter import *
import time
window = Tk()
window.title("Button")
window.geometry("500x300")
def buttonclick():
tex = Label(text="You clicked the button")
tex.pack()
tex.after(3000, tex.destroy)
but = Button(text="Click me!", command=buttonclick)
but.pack()
window.mainloop()
Output:
Your code looks correct for the most part. The reason why it doesn't appear to be working is that there is nothing telling the window to update after adding the text. A simple fix would be to add window.update() when you create the label.
The Code should look like:
from tkinter import *
import time
window = Tk()
window.title("Button")
window.geometry("500x300")
def buttonclick():
tex = Label(text="You clicked the button")
tex.pack()
window.update()
time.sleep(5)
tex.destroy()
window.update()
but = Button(text="Click me!", command=buttonclick)
but.pack()
window.mainloop()
Noob Alert!!!
Hello, I just started my journey with through python a couple of days ago, so my question will most likely be extremely simple to answer. Basically I have a random number gen. from 1 to 10, this is activated when the "test button is pressed on the windows that pops up. As you can see from the image below the random number appears in the output console on the bottom of the screen, in this case it was a 9. So here's the question, How can I make the random number on the GUI? so when the button is pressed a random number appears on the same window as the button.
https://i.stack.imgur.com/hWd3i.png
Any help is appreciated!
from tkinter import *
root = Tk()
root.geometry("300x300")
root.title("test it is")
root.grid()
def randnum(event):
import random
value =random.randint(1,10)
print(value)
button_1 = Button(root, text="test")
button_1.bind("<Button-1>",randnum)
button_1.pack()
root.mainloop()
from tkinter import *
root = Tk()
root.geometry("300x300")
root.title("test it is")
root.grid()
def randnum(event):
import random
value =random.randint(1,10)
print(value)
updateDisplay(value)
def updateDisplay(myString):
displayVariable.set(myString)
button_1 = Button(root, text="test")
button_1.bind("<Button-1>",randnum)
button_1.pack()
displayVariable = StringVar()
displayLabel = Label(root, textvariable=displayVariable)
displayLabel.pack()
root.mainloop()
Here is what it looks like.You have to create a Label with a Button, whose value will get updated when you click on button.
import tkinter as tk
from random import randint
win = tk.Tk()
def test_button_click():
label_val.set(randint(1, 10))
my_button = tk.Button(win, text='Test Button',
command=test_button_click)
my_button.grid(column=0, row=0)
label_val = tk.IntVar()
my_Label = tk.Label(win, textvariable=label_val)
my_Label.grid(column=1, row=0)
win.mainloop()
This will achieve what you are requesting -- create a tk window, add a button and label, use the callback test_button_click to set the labels int var when the button is clicked.
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()