Can I randomly select a Tkinter Checkbutton programmatically?

General Tech Learning Aids/Tools . 2 years ago

  0   2   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 5 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Write Your Comments or Explanations to Help Others



Tuteehub forum answer Answers (2)


profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

Disclaimer - Brand new to programming, with Python (3.x) being my choice of language to begin with.

I recently completed a Python course on codeacademy in which I created a mini battleships game. As a learning project I'm developing a GUI using Tkinter and Checkbuttons for the game board so that it can be played outside of the interpreter.

I'm now blocked as I don't know how to have Python randomly select the Checkbutton position for the battleship location - and keep it invisible from the player.

Here's my Tkinter code so far...

from tkinter import *

class gui(object):
    def __init__(self):

        master = Tk()
        master.title("Battleships!")

        w = Label(master, text="Let's play Battleships!")
        w.pack()
        w = Label(master, text="Click in a box to try and bomb the 
Battleship:")
        w.pack()

        self.checkvar1 = IntVar()

        c = Checkbutton(master, variable=self.checkvar1, command=self.cb)
        c.pack()

        #Hopefully I can use this to display messages to user as they guess
        w = Label(master, text=" ")
        w.pack()

        Button(master, text="I give up!", command=master.quit).pack()

        master.mainloop()

    def cb(self):
        print("variable is", self.checkvar1.get())
        return self.checkvar1.get()

def main():
    g = gui()

if __name__ == '__main__':
    main()

Perhaps I should first create my Checkbutton Grid (5 x 5) instead of just trying it with a single Checkbutton (I was attempting to slice up my development into logical problems)?

Here is the original game code (no GUI)...

from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print(" ".join(row))

print("Let's play Battleship!")
print_board(board)

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board
                                                    
                                                    
0 views   0 shares
profilepic.png
manpreet 2 years ago

You could always generate all your checkboxes (while storing them in a list when you create them) and then choose an item randomly in the list, store it, and when the user clicks, compare the one he clicked to the one you've stored.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community