Python Forum
Need tkinter help with clicking buttons
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need tkinter help with clicking buttons
#1
In the code below I have images of 3 dice (1,2,3) randomly selected and I have the on_click method in place to change the background to red for that die whenever the button for that die is clicked. However, I also want to print the value of that die when it is clicked. For example if 2 is clicked I want to print 2. What is happening when I click on one die it prints the value of all 3 dice randomly selected. So if the 3 random dice selected were 2,3,1 and I click on 2 it prints [[2], [3], [1]] instead of just 2. What do I need to change in my on_click method to fix this?
import random
from tkinter import *
from PIL import ImageTk, Image
import tkinter as tk
from tkinter import messagebox
from tkinter import simpledialog
from tkinter.messagebox import showinfo

root = Tk()


def on_click(event):
    event.widget.config(bg='red')

    buttons = [btn1, btn2, btn3]

    diceclicked = []
    for b in buttons:
        diceclicked.append([value for (key, value) in r.items() if b.cget('image') == key])
    print(diceclicked)

global dice1
dice1 = ImageTk.PhotoImage(Image.open("C:\\Users\\Dan\\Desktop\\Alea_1.png"))

global dice2
dice2 = ImageTk.PhotoImage(Image.open("C:\\Users\\Dan\\Desktop\\Alea_2.png"))

global dice3
dice3 = ImageTk.PhotoImage(Image.open("C:\\Users\\Dan\\Desktop\\Alea_3.png"))


r = {'pyimage1':1,'pyimage2':2,'pyimage3':3}

dice = [dice1,dice2,dice3]

rdice = random.choice(dice)
rdice2 = random.choice(dice)
rdice3 = random.choice(dice)


btn1 = Button(root, image=rdice)
btn1.pack()
btn1.bind('<Button-1>', on_click)

btn2 = Button(root,image=rdice2)
btn2.pack()
btn2.bind('<Button-1>', on_click)

btn3 = Button(root,image=rdice3)
btn3.pack()
btn3.bind('<Button-1>',on_click)

root.mainloop()
Reply
#2
for buttons use:
def on_click(btn_no):
...

btn1 = Button(root, image=rdice, command=lambda b=1: on_click(b))
btn1.pack()
btn1 = Button(root, image=rdice2, command=lambda b=2: on_click(b))
btn2.pack()
btn1 = Button(root, image=rdice3, command=lambda b=3: on_click(b))
btn3.pack()
get rid of the globals
don't use absolute paths for images
Reply
#3
Your other option is to use functools module and partial
import tkinter as tk
from functools import partial
class Yahtzee(tk.Frame):
    def __init__(self, parent=None):
        ....
    def make_dice(self):
        for pic in picture_list:
            btn=tk.Button(parent, image= pic, command= partial(when_clicked,arguments)
            btn.pack(side=left)
            ......
    def when_clicked(*args):
        ...
            
pretty sure I gave you a class template on your last post, search saving your photo images so they won't get garbage collected, Start making lists, use loops, zip list into dictionaries. Lists are static they won't change position. refract and simplify.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter toggle buttons not working Nu2Python 26 12,226 Jan-23-2022, 06:49 PM
Last Post: Nu2Python
  TkInter Binding Buttons ifigazsi 5 9,222 Apr-06-2020, 08:30 AM
Last Post: ifigazsi
  Issue on tkinter with buttons Reldaing 1 3,052 Jan-07-2020, 08:21 AM
Last Post: berckut72
  Tkinter Buttons action d3fi 1 2,808 Nov-20-2019, 09:16 PM
Last Post: Larz60+
  Nesting menu buttons tkinter for python Mocap 1 3,363 Jul-18-2019, 11:46 AM
Last Post: metulburr
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 8,789 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  tkinter button acts normal ONLY after clicking off the window justin_m 8 7,844 Apr-24-2018, 06:14 AM
Last Post: Larz60+
  [Tkinter] tkinter freezes by clicking button Zatox11 33 32,787 Apr-10-2018, 09:03 AM
Last Post: Zatox11
  [Tkinter] Aligning Python Tkinter Buttons In Rows TollyThaWally 3 9,108 Feb-22-2018, 02:56 AM
Last Post: woooee
  Multi windows in tkinter buttons not responding correctly curtjohn86 13 14,140 Jul-01-2017, 03:42 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020