Python Forum
code is not working , can anybody help?
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code is not working , can anybody help?
#5
tkinter is very forgiving of programmer stupidity. It is amazing how wrong your code can be and it will draw pretty windows. Believe me, I have been stupid plenty of times. Six times already while working on this post.

There are three reasons your button command does not work. Button expects command to be a function and you are passing a string. The second problem is that playermove(kare1,0,0) is not a function. It is a function call, and the button command will be bound to the return value of the function. The third problem is that 'kare1' is not yet defined when this code is executed.

The most common way to bind a button command to a function with parameters is to use a lambda expression.
command=lambda b=kare1:playermove(b, 0, 0)
You may say "But that is a function call! I can tell because of the parenthesis!" You are correct, there is a function call, but we are not binding command to the function call, we are binding it to the lambda expression. When the button is pressed the code calls the lambda expression, and the lambda expression calls the function. I know that may be a bit fuzzy right now, but you need to learn how these lambda expressions work because you will use them a lot if you write GUI applications.

If you want to pass your button as an argument to a function, you need to first define the button. This means you need to split creating the button and binding the button.
kare1 = tkinter.Button(text=' ', fg='yellow', bg='red')
kare1.configure('command=lambda b=kare1: playermove(b, 0, 0))
Your button should now call playermove() with the appropriate arguments. Now you need to fix playermove().
def playermove(buton,x1,y1):
    if play == 1:
        play = 2
        buton = tkinter.Label(text='            X\n            ',command='',fg='white',bg='black')
        buton.place(x=x1,y=y1)
    elif play == 2:
        play = 1
        button = tkinter.Label(text='            O\n            ',command='',fg='white',bg='black')
        buton.place(x=x1,y=y1)
You are passing a button to the function, but you don't use it. Instead you make a label and place it over the button. What you should do is change the buton text.
def playermove(buton):
    global play
    marker = 'X' if play == 1 else 'O'
    buton['text'] = marker
    play = 1 if play == 2 else 2
Since you are not making a label you don't need to pass the button coordinates. This simplifies the function and the function binding for the button.

Also notice that I declared "play" to be a global variable. Variables defined inside a function are by default saved in the function namespace. When the function returns, the variable ceases to exist. If you want to change "play" inside playermove and have that change be visible the next time playermove() is called, "play" needs to be a global variable. The way to tell a function that a variable is global is to use the "global" keyword followed by the function name.

Finally, there are better ways to make large buttons than what you are trying. If you want a big button, make the text large by using a large font. You can also specify the width and height of the button. While working on your code I made my own version. This is how I made my buttons:
    button = tk.Button(pencere, text=' ', font=('Times', 32, 'bold'),
                       fg='yellow', bg='red', width=4, height=2)
    button.configure(command=lambda b=button: playermove(b))
Setting the font size = 32 makes really large buttons. To make the buttons even larger I make them 4 characters wide and 2 characters high.
Reply


Messages In This Thread
RE: code is not working , can anybody help? - by deanhystad - Mar-22-2021, 04:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 960 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 890 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,016 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,077 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,928 Mar-21-2022, 10:12 AM
Last Post: End3r
  I don't undestand why my code isn't working. RuyCab 2 1,988 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  Short code for EventGhost not working Patricia 8 3,680 Feb-09-2021, 07:49 PM
Last Post: Patricia
  Code no longer working yk303 14 10,173 Dec-21-2020, 10:58 PM
Last Post: bowlofred
  autocomplete working code sample not working... aviper4u 0 1,639 Oct-24-2020, 03:04 AM
Last Post: aviper4u
  code not working, NameError: name 's' is not defined ridgerunnersjw 4 3,820 Oct-05-2020, 07:03 PM
Last Post: buran

Forum Jump:

User Panel Messages

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