Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import tkinter as tk
  2. import math
  3.  
  4.  
  5. class App(tk.Frame):
  6. def __init__(self, bx, by, bsize, master=None):
  7. super().__init__(master)
  8.  
  9. self.buttons = []
  10.  
  11. self.bx, self.by = bx, by
  12. self.width, self.height = bx * bsize, by * bsize
  13.  
  14. self.init_gui()
  15.  
  16. def init_gui(self):
  17. self.grid(sticky=tk.E + tk.W + tk.N + tk.S)
  18. self.grid_propagate(False)
  19. self.config(width=self.width, height=self.height)
  20.  
  21. for i in range(self.bx * self.by):
  22. self.master.columnconfigure(math.floor(i % self.bx), weight=1)
  23. self.master.rowconfigure(math.floor(i / self.by), weight=1)
  24.  
  25. self.make_buttons()
  26.  
  27.  
  28. def create_button(self, location_name, row, column):
  29. self.columnconfigure(column, weight=1)
  30. self.rowconfigure(row, weight=1)
  31.  
  32. button = tk.Button(self, text=location_name, command=lambda: self.action(column, row))
  33. button.grid(row=row, column=column, sticky=tk.E + tk.W + tk.N + tk.S)
  34.  
  35. return button
  36.  
  37. def make_buttons(self):
  38. for i in range(self.bx * self.by):
  39. self.buttons.append(self.create_button(i+1, int(i % self.bx)+1, int(i / self.by)+1))
  40.  
  41. def get_button(self, x, y):
  42. return self.buttons[x * self.grid_size + y]
  43.  
  44. def action(self, x, y):
  45. print("{},{}\n".format(x, y))
  46.  
  47.  
  48. if __name__ == '__main__':
  49. root = tk.Tk()
  50. # Call the app with the number of buttons you want x,y and the size of each button in pixels
  51. app = App(20, 20, 24, master=root)
  52.  
  53. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement