N
N
NickTheBuilder2019-06-01 20:39:29
Python
NickTheBuilder, 2019-06-01 20:39:29

How to stop a tkinter window from opening endlessly?

I have a game program where you need to turn over the cards and find matches. As soon as a matching pair is found, a window with a question should open (the question will be directed to the audience and does not require a written answer). Then I close the window myself and the game should continue. The problem is that the window (written in tkinter) opens indefinitely, that is, I close it, and it pops up again, help, how to solve this problem?
MAIN CODE

import os, random, time, pygame
from tkinter import *
from second import Question

pygame.init()
SCREEN = (710,450)
ICON = pygame.image.load(os.path.join("memory.ico"))
pygame.display.set_icon(ICON)
pygame.display.set_caption("Игра")
DISPLAY = pygame.display.set_mode(SCREEN)

#Define objects and generate number grid
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
ARIAL_200 = pygame.font.SysFont("Arial", 200)
ARIAL_50 = pygame.font.SysFont("Arial", 50)
ARIAL_35 = pygame.font.SysFont("Arial", 35)
ARIAL_20 = pygame.font.SysFont("Arial", 20)
CARD_LEN = 100
CARD_MARGIN = 10
CARD_HOR_PAD = 37
CARD_VER_PAD = 22
ROWS = 4
COLS = 5
cards = [i for i in range(10) for j in range(2)]
random.shuffle(cards)
CARD_VAL_GRID = [cards[i*len(cards) // ROWS:(i+1)*len(cards) // ROWS] for i in range(ROWS)]
CARD_GRID = [[] for i in range(ROWS)]
for i in range(ROWS):
    if i == 0:
        for j in range(COLS):
            if j == 0:
                CARD_GRID[i].append(pygame.Rect(CARD_MARGIN, CARD_MARGIN, CARD_LEN, CARD_LEN))
            else:
                CARD_GRID[i].append(pygame.Rect(CARD_GRID[i][j-1].x + CARD_LEN + CARD_MARGIN, CARD_MARGIN, CARD_LEN, CARD_LEN))
    else:
        for j in range(COLS):
            if j == 0:
                CARD_GRID[i].append(pygame.Rect(CARD_MARGIN, CARD_GRID[i-1][0].y + CARD_LEN + CARD_MARGIN, CARD_LEN, CARD_LEN))
            else:
                CARD_GRID[i].append(pygame.Rect(CARD_GRID[i][j-1].x + CARD_LEN + CARD_MARGIN, CARD_GRID[i-1][0].y + CARD_LEN + CARD_MARGIN, CARD_LEN, CARD_LEN))
global exposed
exposed = []
global matched
matched = []
global wrong
wrong = []
global turns
turns = 0

#Game loop
while True:
    for event in pygame.event.get():
        #Detect quit
        if event.type == pygame.QUIT:
            pygame.quit()

    #Check for mouse click
    pressed = list(pygame.mouse.get_pressed())
    for i in range(len(pressed)):
        if pressed[i]:
            for i in range(ROWS):
                for j in range(COLS):
                    mouse_pos = list(pygame.mouse.get_pos())
                    if mouse_pos[0] >= CARD_GRID[i][j].x and mouse_pos[1] >= CARD_GRID[i][j].y and mouse_pos[0] <= CARD_GRID[i][j].x + CARD_LEN and mouse_pos[1] <= CARD_GRID[i][j].y + CARD_LEN:
                        global has_instance
                        has_instance = False
                        for k in range(len(exposed)):
                            if exposed[k] == [i, j]:
                                has_instance = True

                        for k in range(len(matched)):
                            if matched[k] == [i, j]:
                                has_instance = True

                        if has_instance == False:
                            exposed.append([i, j])

    if len(exposed) == 2:
        turns += 1
        if CARD_VAL_GRID[exposed[0][0]][exposed[0][1]] == CARD_VAL_GRID[exposed[1][0]][exposed[1][1]]:
            matched.extend(exposed)
            exposed.clear()

        else:
            wrong.extend(exposed)
            exposed.clear()

    #Clear screen
    DISPLAY.fill(BLACK)

    #Draw cards
    for i in range(ROWS):
        for j in range(COLS):
            pygame.draw.rect(DISPLAY, (255, 255, 255), CARD_GRID[i][j])

    #Draw numbers
    if exposed:
        for i in exposed:
            text = str(CARD_VAL_GRID[i[0]][i[1]])
            render = ARIAL_50.render(text, True, BLACK)
            DISPLAY.blit(render, (CARD_GRID[i[0]][i[1]].x + CARD_HOR_PAD, CARD_GRID[i[0]][i[1]].y + CARD_VER_PAD))

    if matched:
        for i in matched:
            text = str(CARD_VAL_GRID[i[0]][i[1]])
            render = ARIAL_50.render(text, True, GREEN)
            DISPLAY.blit(render, (CARD_GRID[i[0]][i[1]].x + CARD_HOR_PAD, CARD_GRID[i[0]][i[1]].y + CARD_VER_PAD))
        Question()
            #import second           
            #import voprosi
            
    if wrong:
        for i in wrong:
            text = str(CARD_VAL_GRID[i[0]][i[1]])
            render = ARIAL_50.render(text, True, RED)
            DISPLAY.blit(render, (CARD_GRID[i[0]][i[1]].x + CARD_HOR_PAD, CARD_GRID[i[0]][i[1]].y + CARD_VER_PAD))

    #Draw other stuff
    title = ARIAL_50.render("Удачи!", True, WHITE)
    DISPLAY.blit(title, (570, 10))
    turn_text = ARIAL_20.render("Попытки: " + str(turns), True, WHITE)
    DISPLAY.blit(turn_text, (580, 75))

    title = ARIAL_20.render("by ОНТ", True, WHITE)
    DISPLAY.blit(title, (630, 390))

    #Check win
    if len(matched) == 20:
        DISPLAY.fill(BLACK)
        win = ARIAL_200.render("Победа!", True, GREEN)
        DISPLAY.blit(win, (40, 105))
        pygame.display.flip()
        break

    pygame.display.flip()
    if wrong:
        time.sleep(1)
        wrong.clear()

WINDOW CODE WITH QUESTION
def Question(): 
    questions = ['Первый', 'Второй', 'Третий', 'Четвертый', 'Пятый', 'Шестой', 'Седьмой', 'Восьмой', 'Девятый', 'Десятый']

    root = Tk()
    root["bg"] = "black"
    root.title("Вопросы")
    root.geometry("640x280")
    
    poetry = "ЗДЕСЬ ВОПРОС"
    label2 = Label(text=poetry, justify=LEFT, font= "Arial 16", bg="yellow")
    label2.place(relx=.2, rely=.3)
    Button(root, text="Quit", command=root.destroy).pack()
 
    root.mainloop()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Kiselev, 2019-06-02
@oceean

You have a while loop in which Question opens at some point. And a new root is created in it. As soon as if matched fires, reset it to matched = False. Switch to qt, don't waste time on tk, you'll appreciate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question