Advertisement
Szizzle

tkinterface.py

Feb 13th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. import os
  2. import pygame
  3. import tkinter as tk
  4. from genetic_stuff import *
  5.  
  6.  
  7. class Application(tk.Frame):
  8.     def __init__(self, master=None):
  9.         super().__init__(master=master)
  10.         self.master = master
  11.         self.pack()
  12.         self.setup()
  13.  
  14.     def setup(self):
  15.         start = tk.Button(self.master, text="Start", command = self.run_simulation)
  16.         self.pause = tk.Button(self.master, text="Pause", command = self.pause_to_resume)
  17.         stop = tk.Button(self.master, text="Stop", command = self.stop_simulation)
  18.  
  19.         db_create = tk.Button(self.master, text="Create DB")
  20.         db_choose = tk.Button(self.master, text="Choose DB")
  21.         replay = tk.Button(self.master, text="Open Replay")
  22.  
  23.         load_obs = tk.Button(self.master, text="Open Map")
  24.         default_obs = tk.Button(self.master, text="Use Default")
  25.         editor_obs = tk.Button(self.master, text="Open Editor", command=self.open_editor)
  26.  
  27.         start.place(x=10, y=10, w=100, h=45)
  28.         self.pause.place(x=125, y=10, w=100, h=45)
  29.         stop.place(x=240, y=10, w=100, h=45)
  30.  
  31.         db_create.place(x=10, y=70, w=100, h=45)
  32.         db_choose.place(x=125, y=70, w=100, h=45)
  33.         replay.place(x=240, y=70, w=100, h=45)
  34.  
  35.         load_obs.place(x=10, y=130, w=100, h=45)
  36.         default_obs.place(x=125, y=130, w=100, h=45)
  37.         editor_obs.place(x=240, y=130, w=100, h=45)
  38.  
  39.     def run_simulation(self):
  40.         self.sim_window = tk.Toplevel(self.master)
  41.         embed = tk.Frame(self.sim_window, width = Main.window_width, height = Main.window_height) #creates embed frame for pygame window
  42.         embed.pack()
  43.         os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
  44.         os.environ['SDL_VIDEODRIVER'] = 'windib'
  45.         run()
  46.         self.sim_window.update()
  47.  
  48.     def pause_to_resume(self):
  49.         Main.pause_simulation()
  50.         if not Main.running:
  51.             self.pause.config(text="Resume")
  52.         else:
  53.             self.pause.config(text="Pause")
  54.  
  55.     def stop_simulation(self):
  56.         try:
  57.             Main.exit_flag = True
  58.             self.sim_window.destroy()
  59.             self.master.update()
  60.         except Exception as e:
  61.             print(e, "exception")
  62.  
  63.     def open_editor(self):
  64.         pygame.init()
  65.         display = pygame.display.set_mode((Main.window_width, Main.window_height))
  66.         display.fill(GREY)
  67.         no_squares = 15
  68.  
  69.         square_width_x = Main.window_width/no_squares
  70.         square_width_y = Main.window_height/no_squares
  71.         x = 0
  72.         y = 0
  73.        
  74.         for row in range(no_squares):
  75.             for column in range(no_squares):
  76.                 pygame.draw.rect(display, (10, 10, 10) ,(x, y, square_width_x, square_width_y), 1)
  77.                 x += square_width_x
  78.             y += square_width_y
  79.             x = 0
  80.  
  81.         pygame.display.update()
  82.         while True:
  83.             for event in pygame.event.get():
  84.                 if event.type == pygame.QUIT:
  85.                     pygame.quit()
  86.                     exit()
  87.         """window = tk.Toplevel(self.master)
  88.        window.title("a second window")
  89.        window.geometry("1000x70")
  90.        window.resizable(False, False)"""
  91.  
  92. root = tk.Tk()
  93. root.title("A window")
  94. root.geometry("350x185")
  95. root.resizable(False, False)
  96.  
  97. app = Application(master=root)
  98. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement