Home > Net >  Is thaht possible to open 2 async windows on Python?
Is thaht possible to open 2 async windows on Python?

Time:08-08

my goal is to open two windows one for content and second for control.

import pygame, sys, tkinter, asyncio
pygame.init()
# Create the window, saving it to a variable.
surface = pygame.display.set_mode((750, 500), pygame.RESIZABLE)
pygame.display.set_caption("Zamkoriada")
stroke = 20
letter_matrix = [['A' for _ in range(29)] for _ in range(10)]


window = tkinter.Tk()
window.title("Login")
window.geometry("200x120")
label = tkinter.Label(window,text="usernane")
inputUser = tkinter.Entry(window)
labelPassword = tkinter.Label(window, text="Password")
inputPassword = tkinter.Entry(window)

button = tkinter.Button(window,text="Go")
label.pack() 
inputUser.pack() 
labelPassword.pack() 
inputPassword.pack()
window.mainloop() 

while True:
    surface.fill((0,0,255))
    # determine responsive width and height of the rectangles
    if surface.get_width() < surface.get_height()*(192/108):
        block_width = (surface.get_width()-125-(28*2))/29
        block_height = block_width*3/2

        ***some code responsible for content***

    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            pygame.quit()
            sys.exit()

        if event.type == pygame.VIDEORESIZE:
            # There's some code to add back window content here.
            surface = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)

Now this code works, as you can probably see in such a way that first when you open it two windows opens and only that from the control works, when you close it, the other one comes back to life. This is because each window requires a loop to work and refresh, but is there any way to make both windows have the same loop or possibly to make the loops execute asynchronously at the same time? Thanks in advance for your help

CodePudding user response:

I found a solution to this problem, it turned out that there is a method in Tkinter that refreshes the window when called, no need to create another loop. Due to the fact that in my program this loop executes very often anyway because it is responsible for the second window, I applied this solution(I used window.update() instead of window.mainloop()):

import pygame, sys, tkinter
pygame.init()
surface = pygame.display.set_mode((800, 600), pygame.RESIZABLE)
pygame.display.set_caption("Familiada")

window = tkinter.Tk()
window.title("Familiada - reżyserka")
window.geometry("400x200")
label = tkinter.Label(window,text="usernane")
inputUser = tkinter.Entry(window)
labelPassword = tkinter.Label(window, text="Password")
inputPassword = tkinter.Entry(window)

button = tkinter.Button(window,text="Go")
label.pack() 
inputUser.pack() 
labelPassword.pack() 
inputPassword.pack()
button.pack()

game1 = Blackboard(20)

game1.fill("B")
game1.write_horizontally('Familiada', 0, 0)

while True:
    surface.fill((0,0,255))
    # ****************************************************************
    # code responsible for grafics
    # ****************************************************************

    #refresh windows
    pygame.display.update()
    window.update() 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            pygame.quit()
            sys.exit()

        if event.type == pygame.VIDEORESIZE:
            # There's some code to add back window content here.
            surface = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)

There is also second solution, using threading, but in my app solution presented above is fine. You can read more about it HERE.

  • Related