I am trying to make a simple drawing program in python using pygame.
I want the user to choose a color, using tkColorChooser.askcolor.
The dialog pops up when the user presses b. It works fine when the user chooses a color. But if the user presses Cancel in the askcolor window, the program goes on running, but the askcolor window does not close.
It stays open on top of the other window, with the Cancel button pressed.
What am I doing wrong?
I am posting a simplified code where the problem appears.
I am running it under Linux Ubuntu 11.10, Python 2.7.2+, python-pygame 1.9.1release-0ubuntu4
Thank you!!
#! /usr/bin/env python
import pygame
from Tkinter import *
import tkColorChooser
def main():
# Colors
black = (0,0,0)
yellow = (252, 229, 3)
bgcolor = black
picturecolor = yellow
running = 1
# Initiate the screen
screen = pygame.display.set_mode((0, 0), pygame.RESIZABLE)
screen.fill(bgcolor)
pygame.draw.circle(screen, picturecolor, (200,200), 10, 0)
pygame.display.flip()
# Initiates the Tk
root = Tk()
root.withdraw()
while running:
event = pygame.event.poll()
if event.type == pygame.KEYUP:
if event.key == pygame.K_b:
ctuple,cstr = tkColorChooser.askcolor(initialcolor=bgcolor, title = 'Choose picture color')
if ctuple != None:
picturecolor = ctuple
screen.fill(bgcolor)
pygame.draw.circle(screen, picturecolor, (200,200), 10, 0)
pygame.display.flip()
if event.key == pygame.K_x:
running = 0
if event.type == pygame.QUIT:
running = 0
if __name__=="__main__":
main()
One thing you seem to be doing wrong is that you’re not starting the event loop by calling
root.mainloop(). Whether that’s the actual problem or not, I don’t know. However, Tkinter isn’t designed to work without the event loop running so it’s not surprising that you’re getting odd behavior.