Advertisement
yoshi_squashy

Proof of concept - pygame with tkinter

May 18th, 2020
1,950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. import tkinter as tk
  2.  
  3.  
  4. class textCollectingWindow():
  5.     def __init__(self,title,linesOfText,prompt,btnText):
  6.         self.linesOfText = linesOfText
  7.        
  8.         self.amActive = True
  9.        
  10.         self.theText = None
  11.        
  12.         self.root = tk.Tk()
  13.         self.root.title(title)
  14.        
  15.         self.promptElement = tk.Label(self.root, text=prompt)
  16.         self.promptElement.pack()
  17.  
  18.         self.entryBox = tk.Text(self.root, height=linesOfText)
  19.         self.entryBox.pack()
  20.        
  21.         self.button = tk.Button(self.root, text = btnText,
  22.                                 command=self.storeText)
  23.         self.button.pack()
  24.  
  25.  
  26.     def updateDisplay(self):
  27.         newLineChars = 0
  28.         try:
  29.             self.root.update()
  30.  
  31.             for char in self.entryBox.get("1.0", tk.END):
  32.                 if char == '\n':
  33.                     newLineChars += 1
  34.  
  35.             if newLineChars == self.linesOfText+1:
  36.                 self.storeText()
  37.         except:
  38.             self.amActive = False
  39.  
  40.        
  41.        
  42.  
  43.     def getState(self):
  44.         return self.amActive
  45.    
  46.     def storeText(self):
  47.         self.theText = self.entryBox.get("1.0", tk.END)
  48.         self.theText = self.theText[:len(self.theText)-1]
  49.         self.root.withdraw()
  50.         self.root.destroy()
  51.  
  52.     def getText(self):
  53.         return self.theText
  54.  
  55.        
  56.  
  57. codeGetter = textCollectingWindow('Code editor',2,'Enter two lines of code:','Add code')
  58.  
  59.  
  60. import pygame
  61. pygame.init()
  62.  
  63. Width = 500; WindowDimensions = (Width, Width)
  64. displayCaption = 'pygame testing display.py'
  65. FPS = 60;
  66. colours = {'yellow':[255,230,0],'light blue':[150, 190, 255],'black':[0,0,0],
  67.            'white':[255,255,255],'orange':[255,200,0]}
  68.  
  69. gameDisplay = pygame.display.set_mode(WindowDimensions)
  70. pygame.display.set_caption(displayCaption)
  71. clock = pygame.time.Clock()
  72.  
  73. close = False
  74.  
  75. i = 0
  76. for k in range(120):
  77.     i += 1
  78.     if i > 100:
  79.         i = 100
  80.  
  81.     for event in pygame.event.get():
  82.         if event.type == pygame.QUIT:
  83.             pygame.quit()
  84.             quit()
  85.         if event.type == pygame.KEYDOWN:
  86.             if event.key == pygame.K_a:
  87.                 close = True
  88.    
  89.     if close == True:
  90.         break
  91.    
  92.     if i < 60:
  93.         gameDisplay.fill(colours['black'])
  94.     elif i < 80:
  95.         gameDisplay.fill(colours['white'])
  96.     else:
  97.         gameDisplay.fill(colours['light blue'])
  98.  
  99.    
  100.     pygame.display.update()
  101.  
  102.     clock.tick(FPS)
  103.  
  104.  
  105.  
  106.  
  107. while True:
  108.     if codeGetter.getState() == False:
  109.         theCode = codeGetter.getText()
  110.         print(theCode)
  111.         break
  112.    
  113.     codeGetter.updateDisplay()
  114.  
  115.  
  116. del codeGetter
  117.  
  118. close = False
  119.  
  120. i = 0
  121. while True:
  122.     i+= 1
  123.     i = i%100
  124.  
  125.     for event in pygame.event.get():
  126.         if event.type == pygame.QUIT:
  127.             pygame.quit()
  128.             quit()
  129.         if event.type == pygame.KEYDOWN:
  130.             if event.key == pygame.K_a:
  131.                 close = True
  132.  
  133.     if close == True:
  134.         break
  135.    
  136.     if i < 50:
  137.         gameDisplay.fill(colours['yellow'])
  138.     else:
  139.         gameDisplay.fill(colours['orange'])
  140.  
  141.     pygame.display.update()
  142.    
  143.     clock.tick(FPS)
  144.  
  145. codeGetter = textCollectingWindow('Code editor',2,'Enter two lines of code:','Add code')
  146.  
  147. while True:
  148.     if codeGetter.getState() == False:
  149.         theCode = codeGetter.getText()
  150.         print(theCode)
  151.         break
  152.    
  153.     codeGetter.updateDisplay()
  154.  
  155.  
  156. while True:
  157.     i+= 1
  158.     i = i%100
  159.  
  160.     for event in pygame.event.get():
  161.         if event.type == pygame.QUIT:
  162.             pygame.quit()
  163.             quit()
  164.        
  165.  
  166.    
  167.    
  168.     if i < 50:
  169.         gameDisplay.fill(colours['yellow'])
  170.     else:
  171.         gameDisplay.fill(colours['orange'])
  172.  
  173.     pygame.display.update()
  174.    
  175.     clock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement