This script is to be a 5x5 grid of buttons, one saying "Click Me" and the rest "Don't Click Me." A correct click scores a point, an incorrect click costs a point. The buttons get shuffled after each click. The player has 60 seconds to maximize his score.
The script is incomplete because I'm having two problems:
1. The individual ttk widget styles are refusing to apply (only the global widget styles are applying).
2. There is an extraneous column at the right of frame2 that I can't get rid of despite monkeying with the row/column/span/configure setttings for a geological age.
Any help appreciated.
from tkinter import Tk
from tkinter.ttk import Frame, Label, Button, Style
from random import randrange
from functools import partial
class Game:
def __init__(self):
self.window = Tk()
self.window.title("Onager Ornery Clicker")
self.createwidgets()
def create_styles(self):
frame_style = Style()
frame_style.theme_use('normal')
frame_style.configure("TFrame",
background='blue',
foreground='gold',
borderwidth='10',
relief='raised')
title_style = Style()
title_style.configure("TLabel", font='serif 30')
instr_style = Style()
instr_style.configure("TLabel", font='serif 12')
def create_frames(self):
self.frame = Frame(self.window)
self.frame.rowconfigure(4)
self.frame.columnconfigure(5)
self.frame.grid(row=0, column=0)
self.frame2 = Frame(self.window)
self.frame2.rowconfigure(5)
self.frame2.columnconfigure(5)
self.frame2.grid(row=4, column=0)
def create_title(self):
self.title = Label(self.window,
style="title_style.TLabel",
text='Onager Ornery Clicker')
self.title.grid(row=0, column=0, columnspan=5)
def create_instr(self):
self.instr = """ Correct click scores a point.
Wrong click costs a point.
You have 60 seconds.
Click Start to begin.
HAVE FUN!"""
self.instr = Label(self.window,
style="instr_style.TLabel",
text=self.instr)
self.instr.grid(row=1, column=0, rowspan=3, columnspan=4)
def create_start_button(self):
self.start = Button(self.window, text='Start')
self.start.grid(row=1, column=4)
def create_timer(self):
self.timer = Label(self.window, text='Time')
self.timer.grid(row=2, column=4)
def create_score(self):
self.score = Label(self.window, text='Score')
self.score.grid(row=3, column=4)
def shuffle(self):
for i, button in enumerate(self.buttons, 1):
button['text'] = "Don't Click Me!"
index = randrange(len(self.buttons))
self.buttons[index]['text'] = "Click Me!"
def create_buttons(self):
self.buttons = []
for item in range(25):
button = Button(self.frame2, width=len("Don't Click me!"))
button['command'] = partial(self.button_clicked, button)
row_num, col_num = divmod(item, 5)
button.grid(row=(row_num+4), column=col_num)
self.buttons.append(button)
self.shuffle()
def button_clicked(self, button):
if button['text'] == "Click Me!":
self.shuffle()
def createwidgets(self):
self.create_styles()
self.create_frames()
self.create_title()
self.create_instr()
self.create_start_button()
self.create_timer()
self.create_score()
self.create_buttons()
clicker = Game()
clicker.window.mainloop()