Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import math
- class App(tk.Frame):
- def __init__(self, bx, by, bsize, master=None):
- super().__init__(master)
- self.buttons = []
- self.bx, self.by = bx, by
- self.width, self.height = bx * bsize, by * bsize
- self.init_gui()
- def init_gui(self):
- self.grid(sticky=tk.E + tk.W + tk.N + tk.S)
- self.grid_propagate(False)
- self.config(width=self.width, height=self.height)
- for i in range(self.bx * self.by):
- self.master.columnconfigure(math.floor(i % self.bx), weight=1)
- self.master.rowconfigure(math.floor(i / self.by), weight=1)
- self.make_buttons()
- def create_button(self, location_name, row, column):
- self.columnconfigure(column, weight=1)
- self.rowconfigure(row, weight=1)
- button = tk.Button(self, text=location_name, command=lambda: self.action(column, row))
- button.grid(row=row, column=column, sticky=tk.E + tk.W + tk.N + tk.S)
- return button
- def make_buttons(self):
- for i in range(self.bx * self.by):
- self.buttons.append(self.create_button(i+1, int(i % self.bx)+1, int(i / self.by)+1))
- def get_button(self, x, y):
- return self.buttons[x * self.grid_size + y]
- def action(self, x, y):
- print("{},{}\n".format(x, y))
- if __name__ == '__main__':
- root = tk.Tk()
- # Call the app with the number of buttons you want x,y and the size of each button in pixels
- app = App(20, 20, 24, master=root)
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement