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?
#1
so, i was making a tic-tac toe program with tkinter . but when i open the program,and press a button;main window dont change.
here is the code:
import tkinter
play = 1


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)


pencere = tkinter.Tk()
pencere.title('tic tac toe')
pencere.geometry("100x100+250+120")
pencere.configure(bg="green")

kare1 = tkinter.Button(text='             \n            ',command='playermove(kare1,0,0)',fg='yellow',bg='red')
kare2 = tkinter.Button(text='             \n            ',command='playermove(kare2,40,0)',fg='yellow',bg='red')
kare3 = tkinter.Button(text='             \n            ',command='playermove(kare3,80,0)',fg='yellow',bg='red')
kare4 = tkinter.Button(text='             \n            ',command='playermove(kare4,0,30)',fg='yellow',bg='red')
kare5 = tkinter.Button(text='             \n            ',command='playermove(kare5,40,30)',fg='yellow',bg='red')
kare6 = tkinter.Button(text='             \n            ',command='playermove(kare6,80,30)',fg='yellow',bg='red')
kare7 = tkinter.Button(text='             \n            ',command='playermove(kare7,0,60)',fg='yellow',bg='red')
kare8 = tkinter.Button(text='             \n            ',command='playermove(kare8,40,60)',fg='yellow',bg='red')
kare9 = tkinter.Button(text='             \n            ',command='playermove(kare9,80,60)',fg='yellow',bg='red')

kare1.place(x=0,y=0)
kare2.place(x=40,y=0)
kare3.place(x=80,y=0)
kare4.place(x=0,y=30)
kare5.place(x=40,y=30)
kare6.place(x=80,y=30)
kare7.place(x=0,y=60)
kare8.place(x=40,y=60)
kare9.place(x=80,y=60)

pencere.mainloop()
Reply
#2
Wall
Reply
#3
I don't use Tkinter, but a quick look at the docs suggest you should not have the playermove in quotes in lines 21-29. Should be command=playermove(kare1,0,0) without the quote.
Reply
#4
(Mar-22-2021, 12:09 PM)jefsummers Wrote: I don't use Tkinter, but a quick look at the docs suggest you should not have the playermove in quotes in lines 21-29. Should be command=playermove(kare1,0,0) without the quote.

tkinter works like i did . But i will try it.thanks
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Illegal instruction? working code for months? korenron 4 2,258 Aug-05-2021, 09:57 AM
Last Post: korenron
  I don't undestand why my code isn't working. RuyCab 2 721 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  Short code for EventGhost not working Patricia 8 1,376 Feb-09-2021, 07:49 PM
Last Post: Patricia
  Code no longer working yk303 14 3,580 Dec-21-2020, 10:58 PM
Last Post: bowlofred
  autocomplete working code sample not working... aviper4u 0 779 Oct-24-2020, 03:04 AM
Last Post: aviper4u
  code not working, NameError: name 's' is not defined ridgerunnersjw 4 1,441 Oct-05-2020, 07:03 PM
Last Post: buran
  Help me get this image converter code working? NeTgHoSt 0 1,004 Jul-14-2020, 10:36 PM
Last Post: NeTgHoSt
  Working code to build GPX from geotagged JPGs? Winfried 3 1,255 May-10-2020, 08:58 PM
Last Post: Winfried
  (Complete Novice) Code not working PythonGainz 9 2,163 Mar-21-2020, 05:53 PM
Last Post: buran
  Prime code not working? NewGuy12 2 1,149 Mar-07-2020, 12:35 AM
Last Post: NewGuy12

Forum Jump:

User Panel Messages

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