#πŸ”’ Tkinter button problems

34 messages Β· Page 1 of 1 (latest)

runic solstice
#

Hi I am struggling with some code at the minute. I'm trying to make a noughts and crosses game in tkinter and am struggling to make the buttons work.

Any help appreciated

Error message:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python313\Lib\tkinter_init_.py", line 2068, in call
return self.func(*args)
~~~~~~~~~^^^^^^^
TypeError: call_make_turn.<locals>.wrapper() missing 1 required positional argument: 'x'

Code below:
https://paste.pythondiscord.com/JJYA

round coralBOT
#

@runic solstice

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

runic solstice
#

ooh

#

wdym?

spiral wind
#

also if i was you, i'd put all of this into a loop
-# fixed url

spiral wind
#

either way, your error is here

runic solstice
#

what do you need?

spiral wind
stable geode
spiral wind
stable geode
# runic solstice why?

how are you learning python? it seems like you dont know how to use classes yet, but are trying to. perhaps a resource that'd explain it would help
(the wrapper thing is also obviously useless, you take the game as an argument and do nothing with it, and its recursive)

runic solstice
runic solstice
spiral wind
#

misleading name

stable geode
#

classes are supposed to be instantiated

runic solstice
#

yeah

#

so what are you guys suggesting?
@stable geode @spiral wind

spiral wind
spiral wind
#

you also dont need posbuttonwidth - you can just say button_width

#

or just hardcode it

#

i'd also change your grid to be an array instead of a dictionary because there's no point to making it a dictionary

runic solstice
#

I just prefer avoiding nested for loops... they kill my brain when debugging

spiral wind
#

whats hard to debug about them?

runic solstice
#

I just always land up getting myself confused with nested for loops

#

coding them is fine... debugging gets confusing for me

stable geode
#

heh, i've never actually used tkinter because im not a fan of ui stuff, but i just wanted to see whether you can build nice abstractions with it and it seems like yeah

from tkinter import Tk, Button, StringVar

class Game(Tk):
    def __init__(self) -> None:
        Tk.__init__(self)
        self.grid = [[GridButton(self, row, col) for col in range(3)] for row in range(3)]
        self.player = 0
    def make_turn(self, row: int, col: int) -> None:
        var = self.grid[row][col].var
        if var.get() == "-":
            var.set("XO"[self.player])
            self.player = (self.player + 1) % 2

class GridButton(Button):
    def __init__(self, game: Game, /, row: int, col: int) -> None:
        self.var = StringVar(value = "-")
        Button.__init__(
            self,
            master = game, 
            textvariable = self.var,
            width = 20,
            height = 10,
            command = lambda: game.make_turn(row, col),
        )
        self.grid(row = row, column = col)

if __name__ == "__main__":
    Game().mainloop()

turns out its not even that bad

round coralBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.