Python Forum

Full Version: popup, text input with two readable buttons
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need a pop-up message that will take a text input. I need a button to submit the input and one to abort the entire program. I have one solution, but I would rather have one that I am not paying $99 per year for a pop-up. Here is what I have. Could you guys show me another way to do the pop-up?

for x in range (numberOfFiles):
    row_data = read_data.iloc[x]
    ts = row_data.iloc[0]
    if ts == target_ts:
        target_row = x
        break

    if x == (numberOfFiles - 1):
        bad_ts = True

        while bad_ts:

             # setup message box
            layout = [[sg.Text('Invalid TS- number')],
                      [sg.Text('Enter valid number'), sg.InputText()],
                      [sg.Button('Submit'), sg.Button('Abort Test')]]

            # Create the Window
            window = sg.Window('Window Title', layout)

            # Event Loop to process "events" and get the "values" of the inputs
            while True:
                event, values = window.read()
                if event == sg.WIN_CLOSED or event == 'Abort Test':  # if user closes window or clicks cancel
                    if event ==  'Abort Test':
                        exit()
                    break

                target_ts = values[0]
                window.close()[/color]

            for x in range(numberOfFiles):
                row_data = read_data.iloc[x]
                ts = row_data.iloc[0]
                if ts == target_ts:
                    target_row = x
                    bad_ts = False
                    break
[Image: pop-up.jpg][/url][/img]
What is sg?
(Aug-27-2024, 05:31 PM)menator01 Wrote: [ -> ]What is sg?

My question got closed from another forum because I used the names of packages that I had tried.

import PySimpleGUI as sg
To be honest I would use tkinter. I can't see paying for something that I can get for free and seems to be easier to use.
Tkinter may be old but, is very easy to learn.
(Aug-27-2024, 06:10 PM)menator01 Wrote: [ -> ]To be honest I would use tkinter. I can't see paying for something that I can get for free and seems to be easier to use.
Tkinter may be old but, is very easy to learn.

Looking at the documentation for tkinter, pyqt5 and pyqt6; It looks like you have the choice of simple message boxes that do not have the capability or it is a full GUI app. That is why I am asking here. Or am I reading the docs and tutorials incorrectly? All I need is one fairly simple pop-up, and I do not want the be forced into re-structuring my entire program to be GUI driven.
You may could do something like this. It's not the best approach but should give an idea.

import tkinter as tk

class Popup:
    def __init__(self, parent):
        self.parent = parent

        label = tk.Label(parent, text='Popup Window')
        label.grid(column=0, columnspan=2, row=0, sticky='new')
        label.configure(font=(None, 20, 'bold'))

        self.msg = tk.Label(parent)
        self.msg.grid(column=0, columnspan=2, row=1, sticky='new', padx=4, pady=4)
        self.msg.configure(
            highlightbackground = '#999999',
            highlightcolor = '#999999',
            highlightthickness = 1,
            bg = '#eeeeee'
        )

        label = tk.Label(parent, text='Number:')
        label.grid(column=0, row=2, sticky='new', pady=4, padx=2)

        self.entry = tk.Entry(parent)
        self.entry.grid(column=1, row=2, sticky='new', padx=2)

        btnframe = tk.Frame(parent)
        btnframe.grid(column=0, columnspan=2, row=3, sticky='new', pady=4)
        btnframe.columnconfigure(0, weight=3, uniform='btn')
        btnframe.columnconfigure(1, weight=3, uniform='btn')

        btn1 = tk.Button(btnframe, text='Submit', command=self.validate)
        btn1.grid(column=0, row=0, padx=2, pady=4)

        btn2 = tk.Button(btnframe, text='Cancel', command=parent.destroy)
        btn2.grid(column=1, row=0, padx=2, pady=4)

    def validate(self):
        number = self.entry.get()
        valid_numbers = [5,12,30,15]
        try:
            if int(number) not in valid_numbers:
                self.entry.delete(0, 'end')
                self.msg.configure(text=f'{number} is not valid')       
            else:
                self.parent.destroy()
        except ValueError:
            self.msg.configure(text='Please enter a number')
            self.entry.delete(0, 'end')


for i in range(100):
    if i == 35:
        root = tk.Tk()
        Popup(root)
        root.mainloop()
    else:
        print(i)
Thanks, that gives me a starting point!
Subclass tkinter SimpleDialog to make any dialog you want.

https://docs.python.org/3/library/dialog.html