AI Coding in Practice: How to Build GUI Tools Without UI Freezes
This article explains why "build me a GUI tool" is a practical test of AI coding assistants, and why generated desktop apps often freeze during execution. It covers how to write explicit requirements that separate features from constraints, how to structure the architecture around background threads and a message queue, and how to verify the result with a checklist. A batch audio/video extraction tool in Python with tkinter is used as the example, but the practices are general and apply to any AI coding workflow.
1. Why “Generate a GUI Tool” Tests Real Coding Ability
AI coding tools now produce large amounts of code from a single prompt like “write me a piece of software.” The problems show up as soon as that code runs locally: it compiles but the logic does not hold, features are missing, and most typically the interface freezes completely while a task is running. Mouse clicks do nothing and the window reports “not responding.”
These failures are not random. Desktop GUI programs generally use an event-driven model, where rendering, event dispatch, and user interaction all share the same main thread. Put a long-running task such as media conversion, batch file processing, or a network request directly inside a button callback, and the main thread is occupied for a long time. The event loop cannot continue, so the UI appears dead. The requirement “must not freeze” is therefore really a test of whether the generated solution understands the threading model, not a test of how many lines of code it produces.
This article uses a batch audio/video extraction tool as the example scenario and abstracts a general, product-agnostic practice from it: how to describe requirements, how to constrain the architecture, and how to verify the output.
2. Requirement Description: Turn Implicit Constraints into Explicit Terms
Most failed conversations with AI coding tools come from requirements that list only features and no constraints. A reusable requirement template should contain at least five categories of information.
- Functional scope: which input formats are supported, which output formats are supported, and whether batch processing and directory recursion are needed.
- Tech stack constraints: language, GUI framework, and external command-line components, stated explicitly rather than left to the model.
- Interaction completeness: whether import, start, cancel, and exit entry points are all present, and how the parameter area and list area are organized.
- Non-functional requirements: progress feedback, status messages, error messages, and the ability to terminate.
- Runtime prerequisites: how the program should degrade and notify the user when an external dependency is missing, instead of throwing an exception and exiting.
Example prompt (illustrative, trim as needed):
Build a batch audio/video extraction tool with Python and the standard-library tkinter module:- Support importing common video formats, with output selectable as MP3 / WAV;- The UI must include: a file list, output directory and format settings, start/cancel/exit buttons, a progress bar, and a status bar;- Conversion must run in a background thread; the main thread handles only the UI and the event loop and must not block;- Support recursive directory import, automatic sequence numbers for duplicate file names, and mutually exclusive states for the start and cancel buttons;- On startup, check whether the external transcoding component is available; if it is missing, show a clear message and disable the start button;- All exceptions must be caught and written to the status bar and the log; the app must not crash outright.
The key point of this description is translating “no freezing” into “run in a background thread, main thread only runs the event loop,” and translating “show errors” into “catch exceptions, surface them in the status bar, and keep a log.” The more specific the constraints, the smaller the variance in the generated result.
3. Architecture: Thread Separation and a Task Queue
Whether the generated result runs stably depends on a few general engineering points.
- Decouple tasks from the UI: wrap the long-running logic in a standalone function or worker class called by a background thread; the UI receives progress messages through a queue rather than touching widgets from another thread.
- Progress reporting: the background side computes a ratio from “files completed / total files” and posts messages periodically; the UI polls the queue on a timer and refreshes the progress bar and status bar.
- Cancellable design: use an event flag as a cooperative interruption signal; the background checks the flag at file boundaries, releases resources, and rolls back the UI state.
- Button state machine: disable start and enable cancel while running, then restore afterwards, preventing duplicate submissions from stacking tasks.
- Dependency pre-check: at startup, detect whether the external transcoding component is installed and usable, and offer actionable advice if it is not.
Illustrative code skeleton:
import queueimport threadingclass Worker(threading.Thread):def __init__(self, tasks, out_dir, msg_queue, cancel_event):super().__init__(daemon=True)self.tasks, self.out_dir = tasks, out_dirself.msg_queue, self.cancel_event = msg_queue, cancel_eventdef run(self):total = len(self.tasks)for idx, src in enumerate(self.tasks, 1):if self.cancel_event.is_set():self.msg_queue.put(("cancelled", idx - 1, total))returntry:dst = self.unique_path(src) # append a sequence number for duplicate namesself.convert(src, dst) # call the external transcoding componentself.msg_queue.put(("ok", idx, total))except Exception as exc:self.msg_queue.put(("error", idx, total, str(exc)))def unique_path(self, src):# produce an output path that does not overwrite existing files...def convert(self, src, dst):...
On the UI side, a single timer consumes the queue:
def poll(self):while True:try:msg = self.msg_queue.get_nowait()except queue.Empty:breakself.render(msg) # only the main thread updates widgetsself.after(80, self.poll)
This structure of “compute in the background, communicate through a queue, render on a timer” is the common approach most desktop tools use to stay responsive, and it should be the default constraint when you hand requirements to an AI coding tool.
4. Verification: Break “It Runs” into a Checklist
After the code is generated, do not stop at whether it passes a syntax check. Verify in this order.
- Static check: compiles without errors, no obvious undefined references.
- Dependency check: a missing external component produces a friendly message rather than a crash.
- Empty and boundary input: clicking start with an empty list should be blocked; very large files and very long paths should be handled.
- Interaction check: while a task is running, drag the window and click other buttons; the UI should stay responsive.
- Output check: output files play normally and the sampling parameters match the settings.
- Exception check: deliberately point at a non-writable directory and confirm the error is caught and displayed.
- Idempotency check: repeated runs do not overwrite files and the sequence-number strategy takes effect.
5. Follow-Up Questions and Iteration
A single generation usually delivers only a skeleton; what really makes the difference is the quality of the follow-up questions. Three are worth asking first: what the default output directory is resolved against, whether unfinished temporary files are cleaned up after cancellation, and whether the log contains a list of failed files with reasons. Lock down one variable per round so you can tell whether a change introduced a regression.
6. Cost and Efficiency
In terms of input versus output, the cost of this kind of task comes mainly from model calls during generation and local verification. What matters more in practice is the rework rate: when the requirements are clearly constrained, the first-pass success rate rises sharply and the total time is actually lower. If you use an AI coding platform with model switching, you can match different capability tiers to task complexity, pairing hard reasoning with simple scripts, to balance results against consumption. Platforms from Baidu AI Cloud typically offer access to multiple model types and unified call management, which makes gradual rollout and cost accounting easier for teams.
7. Conclusion
Generating a desktop tool in ten minutes that runs without errors and without a frozen interface is not luck; it is the combined result of requirements engineering and the threading model. Writing non-functional requirements as explicit terms, moving long-running tasks into a background thread, and breaking acceptance into a checkable list — these three steps apply to any AI coding tool, and to code review in team settings as well. Once you have this method, generative tools stop being demo pieces and become actual productivity.
