-2

I want to combine the functions def clicked and def clicked2 because they are the same but I don't know how to do that. I haven't tried yet but I can't think of a way to do it.

from tkinter import *
import random
window = Tk()
x = round(random.random()) + 1
m = 7 
window.title("NIM-7 spel") 
window.geometry('350x200') 
lbl = Label(window, text="Player " + str(x) + " next")
lbl2 = Label(window, text="Their are " + str(m) + " coins left")
lbl.grid(column=0, row=1) 
lbl2.grid(column=1, row=0)

def clicked(): 
    global x
    global m
    m -= 1
    if m < 1:
        lol = "Player " + str(x) + " won!"
        res = ""
    else: 
        if x == 1: 
            x = 2
        else: 
            x = 1
        lol = "Their are " + str(m) + " coins left"
        res = "Player " + str(x) + " is next"
    lbl.configure(text=res) 
    lbl2.configure(text=lol)
def clicked2(): 
    global x
    global m
    m -= 2
    if m < 1: 
        lol = "Player " + str(x) + " won!"
        res = ""
    else: 
        if x == 1: 
            x = 2
        else: 
            x = 1
        lol = "Their are " + str(m) + " coins left"
        res = "Player " + str(x) + " is next"
    lbl.configure(text=res)
    lbl2.configure(text=lol)

btn1 = Button(window, text="Take 1 coin", command=clicked)
btn2 = Button(window, text="Take 2 coins", command=clicked2)
btn1.grid(column=1, row=1)
btn2.grid(column=2, row=1)
window.mainloop() 

Does anyone know how to do it?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Logan
  • 11
  • If they're identical, you can just delete one of them and use the other one. Or do you mean that they are _nearly_ the same? – khelwood Oct 31 '19 at 12:31
  • They are not the same. One decrements m by 1 the other by 2. Do you mean to ask how to factor the common parts into a third function and call that new function from these two? – Abel Oct 31 '19 at 12:34

2 Answers2

1

You can add arguments to tkinter buttons if you use a lambda function. Some identical questions were asked already:

Functions in Tkinter

How to call a function with arguments in "Button" function from "tkinter" python package?

def clicked(value): 
    global x
    global m
    m -= value
    if m < 1:
        lol = "Player " + str(x) + " won!"
        res = ""
    else: 
        if x == 1: 
            x = 2
        else: 
            x = 1
        lol = "Their are " + str(m) + " coins left"
        res = "Player " + str(x) + " is next"
    lbl.configure(text=res) 
    lbl2.configure(text=lol)

btn1 = Button(window, text="Take 1 coin", command = lambda:clicked(1))
btn2 = Button(window, text="Take 2 coins", command = lambda:clicked(2))
Stef van der Zon
  • 633
  • 4
  • 13
  • Thanks for your answer. It was really helpfull. I'm a beginner so it's still hard for me. Do you also know how to make the code in your answer as short as possible? I'd like to learn the most efficient way of coding. – Logan Oct 31 '19 at 15:40
0

Pass in how much you want to decrement m as a parameter. So:

def clicked(decrement): 
    global x
    global m
    m -= decrement
    if m < 1: 
        lol = "Player " + str(x) + " won!"
        res = ""
    else: 
        if x == 1: 
            x = 2
        else: 
            x = 1
        lol = "Their are " + str(m) + " coins left"
        res = "Player " + str(x) + " is next"
    lbl.configure(text=res)
    lbl2.configure(text=lol)

Then call it like this: clicked(1) and clicked(2).

Because you are passing it as a parameter to Button, you can create dummy functions:

def clicked1():
    clicked(1)

and similar for clicked2.

Ollie
  • 1,641
  • 1
  • 13
  • 31
  • Thanks man. I appriciate it. I'm a beginner so it's still really hard for me. Do you also know how to make the code as short as possible? – Logan Oct 31 '19 at 15:27