I am making a random generator for my friends and I'm stuck trying to make a scroll down option. So if you generate more the window can show, a scroll down window should be possible. But I can't seem to get any to work. I've tried many online tutorials.
And my second issue with my code is that I can't clear the generated labels from the window. I got it working that it expands the window.
from cProfile import label
from pickle import FRAME
import random
import tkinter as tk
from tkinter import BOTH, DISABLED, LEFT, RIGHT, VERTICAL, Y, Frame, Label, filedialog, Text
import os
from tkinter import ttk
from tkinter.font import NORMAL
from tkinter.messagebox import YES
root = tk.Tk()
root.title('guesser')
#Pelin arvonta ohjelma !
def delete():
for child in root.children.values():
info = child.grid_info()
if info['column'] == 0:
child.grid_forget()
def arvonta():
global label
list1 = []
lista = ["Valorant","Rainbow","Vampire: The masquerade","Playerunknown's battlegrounds","Fortnite","Left 4 Dead 2","Counter strike Global offensive","Realm roayale","Black ops 1 zombies/multiplayer","Black ops 2 zombies/multiplayer","Black ops 3 zombies/multiplayer"]
numero = random.randint(0, 10)
hahmo = (lista[numero])
list1.append(hahmo)
for app in list1:
label = tk.Label(frame, text=app, bg="red",font=('Helvetica',20))
label.pack()
def valorant():
list2 = []
lista2 = ["Brimstone","Viper","Omen","Killjoy","Cypher","Sova","Sage","phoenix","Jett","Reyna","Raze","Raze","Breach","Skye","Yoru","Astra","Kay/o","Chamber","Neon","Fade"]
numero = random.randint(0, 19)
randomValorantagent=(lista2[numero])
list2.append(randomValorantagent)
for app in list2:
label = tk.Label(frame, text=app, bg="red",font=('Helvetica',20))
label.pack()
def quitter():
quit()
canvas = tk.Canvas(root,height=700,width=700,bg="#263D42")
canvas.pack(side=LEFT,fill=BOTH,expand=1)
frame = tk.Frame(root,bg="green")
frame.place(relwidth=0.8,relheight=0.8,relx=0.1,rely=0.1)
frame.pack(fill=BOTH,expand=1)
my_scrollbar = ttk.Scrollbar(frame, orient=VERTICAL, command=canvas.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)
# Configure The Canvas
canvas.configure(yscrollcommand=my_scrollbar.set)
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion = canvas.bbox("all")))
# Create ANOTHER Frame INSIDE the Canvas
second_frame = Frame(canvas)
# Add that New frame To a Window In The Canvas
canvas.create_window((0,0), window=second_frame, anchor="nw")
#rlls the game
openfile = tk.Button(second_frame,text="Roll a game",padx=10,pady=5,fg="white",bg="#263D42", command=arvonta)
openfile.pack()
#rolls a valorant agent
valorantA = tk.Button(second_frame,text='Roll valorant agent',padx=10,pady=5,fg="white",bg="#263D42",command=valorant)
valorantA.pack()
# stops program
stop = tk.Button(second_frame,text="Quit",padx=10,pady=5,fg="white",bg="#263D42",command=quitter)
stop.pack()
# deletes all info from screen.
deletor = tk.Button(second_frame,text="delete info",padx=10,pady=5,fg="white",bg="#263D42",command=delete)
deletor.pack()
root.mainloop()```
The following does most of what you want. I wrote it some time ago to test Scrollbars because they are wonky IMHO
from tkinter import *
from functools import partial
class ButtonsTest:
def __init__(self):
self.top = Tk()
self.top.title("Click a button to remove")
self.top.geometry("425x200+50+50")
Label(self.top, text=" Click a button to remove it ",
bg="lightyellow", font=('DejaVuSansMono', 12)
).grid(row=0, sticky="nsew")
Button(self.top, text='Exit', bg="orange", width=9,
command=self.top.quit).grid(row=1,column=0,
sticky="nsew")
self.add_scrollbar()
self.button_dic = {}
self.buttons()
self.top.mainloop()
##-------------------------------------------------------------------
def add_scrollbar(self):
self.canv = Canvas(self.top, relief=SUNKEN)
self.canv.config(width=400, height=200)
self.top_frame = Frame(self.canv, height=100)
##---------- scrollregion has to be larger than canvas size
## otherwise it just stays in the visible canvas
self.canv.config(scrollregion=(0,0, 400, 500))
self.canv.config(highlightthickness=0)
ybar = Scrollbar(self.top, width=15, troughcolor="lightblue")
ybar.config(command=self.canv.yview)
## connect the two widgets together
self.canv.config(yscrollcommand=ybar.set)
ybar.grid(row=3, column=2, sticky="ns")
self.canv.grid(row=3, column=0)
self.canv.create_window(1,0, anchor=NW,
window=self.top_frame)
##-------------------------------------------------------------------
def buttons(self):
b_row=1
b_col=0
for but_num in range(1, 51):
## create a button and send the button's number to
## self.cb_handler when the button is pressed
b = Button(self.top_frame, text = str(but_num), width=5,
command=partial(self.cb_handler, but_num))
b.grid(row=b_row, column=b_col)
## dictionary key=button number --> button instance
self.button_dic[but_num] = b
b_col += 1
if b_col > 4:
b_col = 0
b_row += 1
##----------------------------------------------------------------
def cb_handler( self, cb_number ):
print("\ncb_handler", cb_number)
self.button_dic[cb_number].grid_forget()
##===================================================================
BT=ButtonsTest()
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()
This is my code in Tkinter. I tried to show buttons with the image when I select the grid size in tkinter UI. My problem is that when I go to put an image inside a button, it does not display it. List grid2 already have images with using the shuffle method.
Any help is appreciated.
import tkinter
import tkinter.ttk
import random
def startButton():
global my_list, roundNum, tmp_list, startTime
grid_size = combobox1.get()[0]
roundNum = combobox2.get()[0]
gridActorList(my_list)
imageUpdate()
startTime=time.time()
def imageUpdate():
global grid_size
t = []
if grid_size == 2:
photo_1 = tkinter.PhotoImage(file="picture/"+grid2[0]+".png")
photo_2 = tkinter.PhotoImage(file="picture/"+grid2[1]+".png")
photo_3 = tkinter.PhotoImage(file="picture/"+grid2[2]+".png")
photo_4 = tkinter.PhotoImage(file="picture/"+grid2[3]+".png")
for k in range(1,5):
t.append(tkinter.Button(window, image=photo_+str(k)))
for i in range(0,4):
t[i].pack()
window = tkinter.Tk()
window.title('Finding different picture!')
window.geometry('500x400')
#combobox
values1=[str(i)+"x"+str(i) for i in range(2,6)] #grid size
values2=[str(j)+"times" for j in range(1,10,2)] #play time size
combobox1=tkinter.ttk.Combobox(window, height=5, width=15, values=values1,
justify='center', takefocus=True )
combobox2=tkinter.ttk.Combobox(window, height=5, width=15, values=values2,
justify='center', takefocus=True )
combobox1.set("select size")
combobox2.set("select times")
combobox1.place(x=15, y=15)
combobox2.place(x=155, y=15)
#startButton
startBtn = tkinter.Button(window, text='start', command=startButton)
startBtn.place(x=300, y=15)
#variables
my_list = []
roundNnum = 0
window.mainloop()
I've tried to minimize the code. If there are more codes required, I'll edit it.
Your code does not run. This makes it hard to debug. But I can see that you're not saving references to the images you put in the buttons.
As the images are created inside a function they will be garbage collected when the function exits. Fix this by saving a reference to the image with the button widget. Examine the example below:
import tkinter as tk
def create_button():
photo = tk.PhotoImage(file="images/gilliam.png")
image_button = tk.Button(window, image=photo)
image_button.image = photo # Save a reference to the image
image_button.pack()
window = tk.Tk()
create_button()
window.mainloop()
I have a small tkinter GUI, which just contains an image of a thermostat. I have an image for each temperature to make a little animation of the temperature increasing. I have got this working to the point where it loops through all the images succesfully, however after the last image is displayed it just disappears. Here is my code so far
#!/usr/bin/python3
import tkinter as tk
from PIL import ImageTk, Image
import time
root = tk.Tk()
root.geometry("1280x720")
root.configure(background='white')
def loopTemp(maxTemp):
for x in range(0, maxTemp + 1):
if x < 22:
thermostat = ImageTk.PhotoImage(Image.open("thermo_"+str(x)+".png"))
panel.configure(image = thermostat)
panel.configure(text = str(x))
root.update_idletasks()
time.sleep(0.07)
light = ImageTk.PhotoImage(Image.open("light-on.png"))
thermostat = ImageTk.PhotoImage(Image.open("thermo_0.png"))
B = tk.Button(root, text ="Start", command= lambda: loopTemp(21))
panel = tk.Label(root, image = thermostat, text="0", compound=tk.CENTER, font=("Helvetica", 60), fg="white")
panel.configure(background='white')
B.pack()
panel.pack(side=tk.LEFT, fill = "both", expand = "yes")
root.mainloop()
Replace:
...
thermostat = ImageTk.PhotoImage(...)
panel.configure(image = thermostat)
...
with:
...
panel.img = ImageTk.PhotoImage(...)
panel.configure(image = panel.img)
...
In order to keep the reference of the image object wherever panel exists, thus have a consistent image display like suggested in the comment here.
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()