[NFBCS] Python Issue:

charleseblack126 at gmail.com charleseblack126 at gmail.com
Sat Jul 25 17:42:08 UTC 2026


Afternoon:

 

I am running a Python script. I am creating a windows form and getting some
information, returning it to the console program and printing it and saving
it to a couple of programs. The problem is that the windows form will not
receive focus. I am a blind person using JAWS, 2026. Below is the base
program and the windows form. If someone could take a look at this and tell
me wha I am doing wrong, it would be appreciated.

 

Base:

 

# ----------------------------------------------

# Console Program to Launch the Tkinter Form

# ----------------------------------------------

 

# Import the custom Tkinter form

from user_form_module import UserForm

 

def main():

    print("Launching the form... Please fill it out.")

 

    # Create instance of the form

    form = UserForm()

 

    # Run the form and wait for user input (blocks here until submitted)

    name, role, comment = form.run()

 

    # Display returned results in console using correct f-string
interpolation

    print("Form completed! Values returned:")

    print(f"Name:    {name}")

    print(f"Role:    {role}")

    print(f"Comment: {comment}")

 

    # Save results to a text file

    with open("form_results.txt", "w") as f:

        f.write(f"Name: {name}\nRole: {role}\nComment: {comment}\n")

 

    print("Values saved to form_results.txt")

 

if __name__ == "__main__":

    main()

 

windows form:

 

# ------------------------------------------------------

# User Form Module (Windows Focus Bypass & JAWS Ready)

# ------------------------------------------------------

 

import tkinter as tk

from tkinter import ttk

import sys

 

class UserForm:

    def __init__(self):

        self.root = tk.Tk()

        self.root.title("User Information Form")

 

        self.name_value = ""

        self.role_value = ""

        self.comment_value = ""

 

        # Handle window close (X button or Alt+F4)

        self.root.protocol("WM_DELETE_WINDOW", self.on_close)

 

        ttk.Label(self.root, text="Enter your name:").pack(padx=10, pady=2)

        self.name_entry = ttk.Entry(self.root)

        self.name_entry.pack(padx=10, pady=2)

 

        ttk.Label(self.root, text="Select your role:").pack(padx=10, pady=2)

        self.role_combo = ttk.Combobox(self.root, values=["Admin", "User",
"Guest"])

        self.role_combo.pack(padx=10, pady=2)

 

        ttk.Label(self.root, text="Additional comments:").pack(padx=10,
pady=2)

        self.comment_entry = ttk.Entry(self.root)

        self.comment_entry.pack(padx=10, pady=2)

 

        ttk.Button(self.root, text="Submit",
command=self.submit_form).pack(padx=10, pady=10)

 

        # Schedule hard focus takeover once event loop begins

        self.root.after(150, self._force_windows_focus)

 

    def _force_windows_focus(self):

        if sys.platform.startswith("win"):

            import ctypes

            user32 = ctypes.windll.user32

            kernel32 = ctypes.windll.kernel32

 

            # Get thread IDs for current foreground window (the IDE) and
Python GUI window

            fg_hwnd = user32.GetForegroundWindow()

            fg_thread_id = user32.GetWindowThreadProcessId(fg_hwnd, None)

            app_thread_id = kernel32.GetCurrentThreadId()

 

            # Attach thread inputs temporarily so OS allows focus transfer

            if fg_thread_id != app_thread_id:

                user32.AttachThreadInput(app_thread_id, fg_thread_id, True)

                

                # Retrieve the actual Win32 window handle for the Tk root

                hwnd = user32.GetParent(self.root.winfo_id())

                if hwnd == 0:

                    hwnd = self.root.winfo_id()

 

                user32.SetForegroundWindow(hwnd)

                user32.SetFocus(hwnd)

                

                # Detach thread input

                user32.AttachThreadInput(app_thread_id, fg_thread_id, False)

 

        # Tkinter internal focus assignments

        self.root.lift()

        self.name_entry.focus_force()

 

    def submit_form(self):

        self.name_value = self.name_entry.get()

        self.role_value = self.role_combo.get()

        self.comment_value = self.comment_entry.get()

        self.root.quit()

        self.root.destroy()

 

    def on_close(self):

        self.root.quit()

        self.root.destroy()

 

    def run(self):

        self.root.mainloop()

        return self.name_value, self.role_value, self.comment_value

 

Thanks for any help.

 

 

Charles Black

Love God, my wife, my children, and my dog, not necessarily in that order.

 

From: Louis Maher <ljmaher03 at outlook.com> 
Sent: Tuesday, July 16, 2024 10:14 AM
To: nfbcs at nfbnet.org
Subject: 2024 Minutes Of The NFB Computer Science Division

 

Hello,

 

Here are the minutes of the 2024 NFB in Computer Science division annual
meeting. Please let me know about any additions or corrections.

 

Note that I am sending this email to the NFBCS email list, and to all 2024
registered division members. For this reason, many people may receive two
copies of these minutes.

 

 

Regards

Louis Maher

Phone: 713-444-7838

Email: ljmaher03 at outlook.com <mailto:ljmaher03 at outlook.com> 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nfbnet.org/pipermail/nfbcs_nfbnet.org/attachments/20260725/5e95561f/attachment.htm>


More information about the NFBCS mailing list