Search code examples
pythonpython-3.xrandompasswordspysimplegui

Displaying all Results for Prime Numbers on One Popup Window


I am trying to create a password generator in Python. I'm starting off by writing a program to generate prime numbers to use when calculating RSA, and I found a program that I modified to display results through popups via PySimpleGUI. However, I'm having trouble getting all my results on one popup window.

Here's my code so far:

import PySimpleGUI as sg

start = int(sg.popup_get_text("Enter the start of the range: " ))
end = int(sg.popup_get_text("Enter the end of the range: "))

for num in range(start, end + 1):
    if num > 1:
        for i in range(2, int(num**0.5) + 1):
            if(num % i) == 0:
                break
        
        else:
            sg.popup(num)

sg.popup("The process is complete. Press OK to close.")

How can I get all the results to be displayed on one popup window?


Solution

  • You are making a popup for every prime number. Instead, make a list and append all prime numbers to the list, and then display the list in one popup.

    import PySimpleGUI as sg
    
    start = int(sg.popup_get_text("Enter the start of the range: " ))
    end = int(sg.popup_get_text("Enter the end of the range: "))
    
    nums = []
    
    for num in range(start, end, 1):
        if num > 1:
            for i in range(2, int(num**0.5) + 1):
                if (num % i == 0):
                    break
                else:
                    nums.append(num)
    sg.popup(nums)
    sg.popup("The process is complete. Press OK to close.")