Latest / CyberSecurity Summary / Python GUI Programming Cookbook: Use recipes to develop responsive and powerful GUIs using Tkinter
Transcript
- 0:00Speaker 1: Welcome to the deep dive. Today we're diving into a
- 0:04Speaker 1: stack of source material all about building graphical user interfaces,
- 0:08Speaker 1: you know, those clickable windows with buttons and textboxes using Python.
- 0:12Speaker 2: And our main guide for this journey is the second
- 0:14Speaker 2: edition of Python gy Programming Cookbook by Burkhard A. Meyer
- 0:18Speaker 2: from back in twenty seventeen.
- 0:19Speaker 1: Okay, let's unpack this. This book sounds like it's basically
- 0:22Speaker 1: a collection of recipes, right, tackling everything from just like
- 0:25Speaker 1: getting a window to show up, yeah, to making it
- 0:28Speaker 1: interactive with buttons, text fields, organizing complex layouts, handling data.
- 0:33Speaker 2: Dealing with slow operations without freezing your app. That's a
- 0:36Speaker 2: big one.
- 0:36Speaker 1: Oh yeah, connecting to databases, even thinking about different languages
- 0:40Speaker 1: and testing. It seems pretty comprehensive.
- 0:42Speaker 2: It really is. It aims for a practical, hands on feel.
- 0:46Speaker 2: The author, Burkhard Meyer, has something like seventeen years in
- 0:50Speaker 2: the software industry, especially with Python and databases. Okay, worked
- 0:53Speaker 2: at various places. And there was a reviewer input too
- 0:55Speaker 2: from Mohit who also knows Python and security. So it
- 0:59Speaker 2: feels grounded in, you know, actually building stuff, right.
- 1:02Speaker 1: So our mission here is to sift through all these
- 1:04Speaker 1: recipes and concepts. We want to pull out the really
- 1:07Speaker 1: essential techniques, the key ideas, so you can get a
- 1:10Speaker 1: solid grasp of Python gui's without reading well every single line.
- 1:15Speaker 2: Of code, basically giving you the highlights real.
- 1:17Speaker 1: Exactly the informed shortcut. So where does one even begin?
- 1:22Speaker 1: The book starts like any good cookbook with the absolute minimum, right,
- 1:26Speaker 1: getting your first window on screen?
- 1:28Speaker 2: Yeah, using pythons built in kinter library. People usually shorten
- 1:32Speaker 2: it to just TK. Okay, and the starting code is
- 1:34Speaker 2: surprisingly simple. You import kinter you create an instance of
- 1:38Speaker 2: the main window class that's TK dot TK usually assign
- 1:42Speaker 2: it to a variable, say win, and then the absolutely
- 1:45Speaker 2: crucial bit win dot main loop.
- 1:48Speaker 1: The main lo Okay, that sounds fundamental. It's like the
- 1:50Speaker 1: heart of the whole thing totally.
- 1:51Speaker 2: It's the event loop. It just sits there, kind of
- 1:53Speaker 2: waiting for things to happen. Button clicks, keypresses, resizing, Oh.
- 1:57Speaker 1: The events.
- 1:58Speaker 2: Yeah, the main loop process these. Without it, your window
- 2:01Speaker 2: might literally just flash up and disappear because the script finishes.
- 2:05Speaker 2: The main loop keeps it alive and listening.
- 2:07Speaker 1: Got it. So it's not just displaying, it's running the
- 2:09Speaker 1: interactive part. And the book mentions a quick tip about
- 2:12Speaker 1: stopping resizing.
- 2:13Speaker 2: Yeah, simple one. If you want to fix size window,
- 2:16Speaker 2: just win dot resizable zero zero zero, easy way to
- 2:20Speaker 2: lock it down.
- 2:21Speaker 1: Okay, useful. So we have our basic window frame. But
- 2:24Speaker 1: an empty box isn't much of a GUI. You need
- 2:26Speaker 1: stuff inside it. The widgets. What are the first building
- 2:30Speaker 1: blocks the book introduces.
- 2:31Speaker 2: Well, the most basic are probably labels. Just text for
- 2:35Speaker 2: showing information or instructions uses TTK dot label simple text.
- 2:39Speaker 2: Then entry widgets. Those are your standard single line textboxes
- 2:42Speaker 2: for getting user input dtk dot.
- 2:45Speaker 1: Entry right displaying info versus getting input. And the main
- 2:48Speaker 1: way to trigger.
- 2:49Speaker 2: Actions buttons dot ttk dot button. And this is where
- 2:52Speaker 2: callbacks become super important. When you create a button, you
- 2:55Speaker 2: tell it which Python function to call when it's clicked
- 2:58Speaker 2: using the command argument. So you'd have my function name
- 3:01Speaker 2: that links the click event to your code.
- 3:03Speaker 1: So the click is the event command links it to
- 3:06Speaker 1: the callback function precisely.
- 3:08Speaker 2: That function just waits passively until the main loop detects
- 3:12Speaker 2: a click on that button, and then it gets executed.
- 3:15Speaker 1: Nice. The book also mentions usability things like putting the
- 3:19Speaker 1: cursor in the right place automatically YEP.
- 3:21Speaker 2: Using the focus method on a widget. So if you
- 3:24Speaker 2: call my entry dot focus, the cursor starts blinking in
- 3:27Speaker 2: that text box. Little things like that make a.
- 3:29Speaker 1: Difference and disabling widgets.
- 3:31Speaker 2: Yeah, you can set a property when you create it,
- 3:33Speaker 2: like a button, so it appears grayed out and doesn't
- 3:36Speaker 2: respond to clicks.
- 3:37Speaker 1: Okay, what about giving users choices not just text entry? Uh?
- 3:42Speaker 2: Check buttons are covered for simple on off choices like
- 3:44Speaker 2: a settings toggle right. They hold their state checked or unchecked,
- 3:48Speaker 2: usually zero or one using a special variable pickenter dot infar.
- 3:52Speaker 2: You can set the initial state using select an alignment.
- 3:56Speaker 1: You mentioned sticky before.
- 3:57Speaker 2: Yes, sticky tk wf and gets used with check buttons
- 4:01Speaker 2: to make sure they line up nicely on the left
- 4:02Speaker 2: side within their layout. Cell w for.
- 4:05Speaker 1: West got it, and radio buttons if for picking just
- 4:08Speaker 1: one option from a group.
- 4:09Speaker 2: Exactly radio buttons. The key difference is that a group
- 4:13Speaker 2: of radio buttons all share the same kintervariable. When you
- 4:16Speaker 2: click one, its value gets assigned to that variable, and
- 4:19Speaker 2: the others automatically de select ah.
- 4:22Speaker 1: So check buttons each have their own variable. Radio buttons
- 4:25Speaker 1: share one.
- 4:26Speaker 2: That's the crucial distinction for managing their state and selecting
- 4:30Speaker 2: A radio button can also trigger a callback immediately, maybe
- 4:34Speaker 2: to change something else in the GI based on the choice.
- 4:36Speaker 1: Okay, what about selecting numbers or from a list?
- 4:39Speaker 2: Spin box controls are good for that. You get a
- 4:41Speaker 2: little box with up down arrows. Again, cycle through numbers
- 4:44Speaker 2: or even a predefined list of text items.
- 4:46Speaker 1: Can you react when the value changes?
- 4:48Speaker 2: Oh yeah, just like a button, You can attach a
- 4:50Speaker 2: command callback that fires whenever the stin box value is changed.
- 4:54Speaker 2: The book shows an example of displaying the selected value
- 4:57Speaker 2: in say a larger text area.
- 5:00Speaker 1: Larger text area scroll texts exactly.
- 5:02Speaker 2: Scroll text really useful for multiline input or display. The
- 5:06Speaker 2: nice thing is features like word wrapping, wrap ek at
- 5:10Speaker 2: word and the scroll bars appearing automatically. They just work.
- 5:14Speaker 2: You mainly just defined its width and height.
- 5:16Speaker 1: Okay, so we can populate a window with widgets, but
- 5:20Speaker 1: they'll just pile up, right, How do you organize them?
- 5:23Speaker 1: Layout management?
- 5:24Speaker 2: Yeah, that's the next big step arranging things logically. The
- 5:28Speaker 2: core idea the book uses is nesting. You put widgets
- 5:32Speaker 2: inside container.
- 5:33Speaker 1: Fidgets like containers within container pretty much.
- 5:37Speaker 2: Ttk dot label frame is a key example. It's a
- 5:39Speaker 2: container that draws a border around its contents and has
- 5:42Speaker 2: a text label. You put related widgets inside a label frame.
- 5:46Speaker 2: Then you arrange the label frame itself within the main window,
- 5:49Speaker 2: maybe alongside other label frames or widgets. It helps break
- 5:52Speaker 2: down complexity.
- 5:53Speaker 1: And spacing to avoid everything being crammed together.
- 5:55Speaker 2: Patting pad X for horizontal space, patty for vertical space
- 5:59Speaker 2: around each widget. Usually set these when you place the
- 6:01Speaker 2: widget using the layout manager.
- 6:02Speaker 1: The book mentions grid mostly Yes.
- 6:05Speaker 2: The grid layout manager is heavily featured. It arranges widgets
- 6:08Speaker 2: in well a grid of rows and columns. You specify
- 6:12Speaker 2: the row and column for each widget.
- 6:13Speaker 1: And you can tweak padding later too.
- 6:15Speaker 2: The book points out you can use methods like grid
- 6:17Speaker 2: Configure after placing a widget to adjust its padding or
- 6:20Speaker 2: other grid options if needed.
- 6:22Speaker 1: What about when the user resizes the window. Do widgets
- 6:25Speaker 1: just stay fixed or can they stretch.
- 6:27Speaker 2: Ah dynamic expansion. That's where Sticky really comes into play again.
- 6:31Speaker 2: We saw sticky tk dot W for left alignment, but
- 6:34Speaker 2: you can use tk E eastwright, TK in north top,
- 6:37Speaker 2: tks self bottom or combinations like TK dot n SEW
- 6:41Speaker 2: to make a widget stretch and fill the entire grid cell.
- 6:44Speaker 2: It's in horizontally and vertically as the window.
- 6:47Speaker 1: Resizes and spanning multiple cells.
- 6:48Speaker 2: Also crucial, column span two makes a widget take up
- 6:51Speaker 2: two columns. Rospan three takes three rows, combining sicki and columns.
- 6:55Speaker 2: Thin row span gives you really fine grain control over
- 6:58Speaker 2: how your layout adapts.
- 7:00Speaker 1: Grid is the main tool here. What if the GUI
- 7:02Speaker 1: has like tons of options, does it just become one
- 7:05Speaker 1: giant window.
- 7:06Speaker 2: That's where TTK dot notebook comes in. It creates tabbed interfaces.
- 7:09Speaker 2: You put different sets of controls on different tabs, keeps
- 7:12Speaker 2: the main window much cleaner tabs.
- 7:14Speaker 1: Okay, that makes sense for complexity. Moving beyond structure, what
- 7:18Speaker 1: about the look and feel, making it polished? Giving feedback?
- 7:21Speaker 2: Right? Standard pop up messages are a basic need. Kinder
- 7:25Speaker 2: has built in message box functions show info for infos,
- 7:29Speaker 2: show warning for warnings, shower for.
- 7:31Speaker 1: Errors, and they pause the main GUI.
- 7:34Speaker 2: Yes, they're modal. The user has to dismiss the message
- 7:37Speaker 2: box before they can interact with the main window. Again
- 7:40Speaker 2: important behavior.
- 7:41Speaker 1: The book mentions independent message boxes. What's that about.
- 7:44Speaker 2: It's more about maybe structuring your code so the call
- 7:47Speaker 2: to show a message box is in its own function,
- 7:50Speaker 2: perhaps for reuse. There's a small code trick mention to
- 7:53Speaker 2: get the text into the body, not the title. And
- 7:55Speaker 2: also a reminder if you create a tk instance just
- 7:58Speaker 2: to use a message box, might need wind dot withdraw
- 8:01Speaker 2: to hide the blank extra window that kinder.
- 8:03Speaker 1: Creates little details. What about customizing the main window itself
- 8:07Speaker 1: title icon pzps.
- 8:09Speaker 2: Setting the title is just a method call wind dot
- 8:11Speaker 2: title my app. The icon usually needs a specific file
- 8:14Speaker 2: format dot ico on Windows, and you might need the
- 8:17Speaker 2: full paths to the file.
- 8:18Speaker 1: Any other visual tweaks mentioned like three D effects, Yeah.
- 8:22Speaker 2: The relief property comes up again. Setting relief sunken or
- 8:25Speaker 2: relief raised on widgets like frames or labels can give
- 8:28Speaker 2: them that classic embossed or debossed look adds a bit
- 8:32Speaker 2: of depth.
- 8:32Speaker 1: And those little helper texts when you hover over something.
- 8:35Speaker 2: Tooltips, Yeah, the book shows how to implement them. It
- 8:38Speaker 2: actually suggests using an object oriented approach creating a tooltip
- 8:42Speaker 2: class ahop again Yeah, it makes the tooltip logic reusable
- 8:46Speaker 2: and cleaner to attach to different widgets.
- 8:48Speaker 1: Okay, And for tasks that take a while showing progress.
- 8:52Speaker 2: The standard TTK dot progress bar you add it to
- 8:55Speaker 2: your layout and the book shows how to start its
- 8:57Speaker 2: animation when a task begins and stop it when done,
- 9:01Speaker 2: basic visual feedback that something's happening.
- 9:03Speaker 1: This feels like a natural lead into handling data within
- 9:06Speaker 1: the dui and maybe where OP really starts to shine definitely.
- 9:10Speaker 2: First, the book introduces special tinter variable types like stringvar.
- 9:14Speaker 2: These aren't just normal Python strings.
- 9:16Speaker 1: How are they different?
- 9:17Speaker 2: They're designed to be linked directly to widgets. If you
- 9:20Speaker 2: link a stringvar to an entry widget, changing the string
- 9:23Speaker 2: bar in your code automatically updates the text in the
- 9:25Speaker 2: entry box, and typing in the entry box updates the stringvar.
- 9:29Speaker 1: Oh two way binding kind of yeah.
- 9:31Speaker 2: Use methods like dot set to put a value into
- 9:34Speaker 2: the string bar and dot get to recrieve the current
- 9:37Speaker 2: value from it.
- 9:37Speaker 1: And the book notes a quirk about needing a TK
- 9:39Speaker 1: instance even just to create a string bar, even if
- 9:42Speaker 1: you hide the window.
- 9:43Speaker 2: Yeah, it's a bit weird. These ticket tenor variables rely
- 9:46Speaker 2: on the underlying TK machinery being initialized, which happens when
- 9:50Speaker 2: you create TK. So even for non visual use, you
- 9:53Speaker 2: might need root ektktk root dot withdrawal.
- 9:57Speaker 1: So calling dot get on these variables or directly on
- 10:00Speaker 1: a widgets like entry is how you pull the user's
- 10:03Speaker 1: input back into your Python logic.
- 10:05Speaker 2: That's the main way. Once you get the value, it's
- 10:07Speaker 2: just a standard Python string or number or whatever, and
- 10:11Speaker 2: you can work with it. The trick is when to
- 10:13Speaker 2: call dot get, usually inside a callbad function.
- 10:15Speaker 1: The book mentions using module level global variables for simple
- 10:19Speaker 1: data sharing, but then pushes towards op pretty strongly.
- 10:22Speaker 2: Why well, globals are tempting for super simple scripts, quick
- 10:26Speaker 2: and easy, but as soon as your GUI gets even
- 10:29Speaker 2: a little bit complex, tracking which function modified which global
- 10:33Speaker 2: variable becomes a nightmare, really hard to debug OPI. Using
- 10:37Speaker 2: classes provides encapsulation. You bundle the data as attributes of
- 10:41Speaker 2: an object, together with the methods functions belonging to the
- 10:44Speaker 2: object that operate on that data. It keeps things organized,
- 10:48Speaker 2: prevents accidental modification from unrelated code, and makes the whole
- 10:51Speaker 2: thing much easier to maintain and extend later.
- 10:54Speaker 1: So a method is just a function inside a class
- 10:56Speaker 1: that acts on the classes data.
- 10:57Speaker 2: Pretty much, and it usually takes self as it's first argument,
- 11:01Speaker 2: which refers to the specific object instance it's working with.
- 11:04Speaker 2: The book really emphasizes trying to refactor a messy NONOOPI
- 11:08Speaker 2: procedural GUI into oop later is painful. It's much better
- 11:12Speaker 2: to start with an OP structure if your project is
- 11:14Speaker 2: going to be anything beyond trivial.
- 11:16Speaker 1: And this connects back to the event driven nature.
- 11:18Speaker 2: Absolutely, the GUI sits there, an event happens button click,
- 11:23Speaker 2: the main loop directs it to the callback. That callback
- 11:26Speaker 2: is often an OP method self dot on button click,
- 11:29Speaker 2: which then uses self to access the object's data, maybe
- 11:31Speaker 2: calls dot get on some widgets, does calculations, and maybe
- 11:35Speaker 2: calls dot set on other widgets to update the display.
- 11:38Speaker 1: Okay, this next one is where I think a lot
- 11:40Speaker 1: of people, including me, have hit a wall. Initially, you
- 11:43Speaker 1: click a button that app needs to do something slow,
- 11:46Speaker 1: and the whole GUI just freezes.
- 11:48Speaker 2: Ah. Yes, the dreaded unresponsive GUI. It's a classic symptom
- 11:53Speaker 2: of doing long running tasks directly in the main GUI thread.
- 11:56Speaker 2: Why does it happen because basic kinter and many GI
- 11:59Speaker 2: toolkits runs its event loop main loop in a single thread.
- 12:02Speaker 2: If that thread gets busy doing something else like downloading
- 12:05Speaker 2: a big file, running a complex calculation, querying a slow database,
- 12:09Speaker 2: it can't process other events. It can't redraw the window,
- 12:12Speaker 2: respond to clicks anything. It's blocked.
- 12:14Speaker 1: So what's the fix?
- 12:15Speaker 2: Multi threading? You need to run that long running task
- 12:19Speaker 2: in a separate thread. The main thread stays free to
- 12:22Speaker 2: run the main loop and keep the GI responsive while
- 12:25Speaker 2: the worker thread chugs away in the background.
- 12:28Speaker 1: Okay, threads. The book distinguishes between threads and processes.
- 12:31Speaker 2: Briefly, Yes, Processes are more isolated, each with its own memory.
- 12:36Speaker 2: Threads run within the same process and share memory. For
- 12:39Speaker 2: tasks within a single GUI application that need to communicate
- 12:43Speaker 2: back to the GUI, threads are often simpler because sharing
- 12:46Speaker 2: data is easier.
- 12:47Speaker 1: Can that worker thread directly update a techer and widget
- 12:51Speaker 1: like change a labels text when it's done?
- 12:53Speaker 2: Generally No, that's not thread safe. Directly manipulating taker widgets
- 12:57Speaker 2: from a background thread can cause crashes or weird behavior.
- 13:00Speaker 1: So how does the worker thread tell the main GUI thread, Hey,
- 13:03Speaker 1: I'm done, here's the result.
- 13:04Speaker 2: The standard safeway is using Python's q Q. It's a
- 13:08Speaker 2: data structure specifically designed for thread safe communication.
- 13:12Speaker 1: A que like a waiting line exactly.
- 13:15Speaker 2: The worker thread puts its result onto the que mi
- 13:18Speaker 2: que dot dwizzled. The main GUI thread meanwhile periodically checks
- 13:23Speaker 2: the queue mi qu dot get no weight. If there's
- 13:26Speaker 2: something there, it safely pulls the result off and updates
- 13:29Speaker 2: the GUI widgets from within the.
- 13:30Speaker 1: Main thread, so the Q acts as a safe ordered
- 13:33Speaker 1: mailbox between threads.
- 13:34Speaker 2: Perfect analogy. It decouples the threads FIFO first in, first
- 13:39Speaker 2: out means results are typically processed in the order they finish.
- 13:42Speaker 2: The book shows how you can pass a Q object around,
- 13:45Speaker 2: letting different parts of your code, even in separate modules,
- 13:48Speaker 2: feed results back to the GUI thread without freezing it.
- 13:50Speaker 1: And common examples of slow tasks shown are things like
- 13:53Speaker 1: file operations or network calls.
- 13:55Speaker 2: Yes, using kinter dot request dot a filename is quick,
- 13:58Speaker 2: but actually reading or writing a large file could block. Similarly,
- 14:02Speaker 2: using erallib dot request dot eurolopin to fetch web data
- 14:05Speaker 2: definitely needs a thread if the network is slow where
- 14:08Speaker 2: the download is large. The book also stresses using try
- 14:11Speaker 2: except blocks around network code to handle potential errors.
- 14:14Speaker 1: Gracefully makes sense okay, shifting gears to data persistence. The
- 14:17Speaker 1: book dives into connecting to my SQL databases, specifically.
- 14:21Speaker 2: Right, a very common requirement. It uses the miceql dot
- 14:24Speaker 2: connector library, which is the official driver from Oracle.
- 14:27Speaker 1: My school, and the first warning is about security absolutely critical.
- 14:31Speaker 2: The book hammers this home do not put your database
- 14:35Speaker 2: username and password directly in your Python source code. It's
- 14:39Speaker 2: a huge security risk if that code ever gets shared
- 14:42Speaker 2: or exposed.
- 14:43Speaker 1: So what should you do?
- 14:44Speaker 2: Store credential separately? Maybe in a configuration file that's kept
- 14:47Speaker 2: in a secure location, has restricted permissions and is read
- 14:50Speaker 2: by your application at runtime, or use environment variables or
- 14:54Speaker 2: a secrets management system, just not hard coded.
- 14:57Speaker 1: Okay, what basic database operations does it cover?
- 15:00Speaker 2: The core crwould operations select to fetch data, insert to
- 15:04Speaker 2: add rows, update to change existing rows, delete to remove rows.
- 15:08Speaker 2: It also briefly introduces essential database concepts like primary keys,
- 15:12Speaker 2: unique IDs for rows and foreign keys, linking related tables.
- 15:16Speaker 1: And is there a tool mentioned for working with the
- 15:18Speaker 1: database outside of Python.
- 15:20Speaker 2: Yeah, myseql workbench. It's a free GI tool provided by
- 15:23Speaker 2: myseql itself, really helpful during development for designing your database schema,
- 15:28Speaker 2: running test SQL queries, checking data without needing to build
- 15:32Speaker 2: all that into your Python app.
- 15:33Speaker 1: First, Okay, let's talk about making apps for a global audience.
- 15:36Speaker 1: Internationalization and localization I eighteen N and L ten N right.
- 15:41Speaker 2: Internationalization I eighteen N I plus eighteen letters plus N
- 15:45Speaker 2: is about designing your app so it can be adapted.
- 15:47Speaker 2: The main part is separating all user visible text labels,
- 15:51Speaker 2: button text messages from your code logic. How do you
- 15:53Speaker 2: do that? The book shows using a simple Python class
- 15:56Speaker 2: is a I eighteen n that holds dictionaries of strings
- 15:59Speaker 2: for different language maybe self dot strings and like you
- 16:02Speaker 2: think kell okay, then in your guy code instead of
- 16:05Speaker 2: tdk dot label to ttk dot label ticks self dot
- 16:07Speaker 2: I t and n dot gets string, gets string greeting
- 16:10Speaker 2: by changing which language dictionary the I eighten n object
- 16:13Speaker 2: uses eg self dot I ten n dot I ten
- 16:15Speaker 2: nd low the whole GUI can switch language.
- 16:18Speaker 1: That's clever and localization L ten N. That's more than
- 16:21Speaker 1: just language.
- 16:22Speaker 2: Yeah, L ten N L plus ten letters plus n
- 16:25Speaker 2: is the actual adaptation to a specific locale. It includes
- 16:28Speaker 2: things like date time formats M and D d y
- 16:31Speaker 2: y versus D M M y y, currency symbols, number
- 16:34Speaker 2: formatting using commas or periods as decimal separators, and crucially,
- 16:39Speaker 2: time zones.
- 16:40Speaker 1: Time zones sound tricky.
- 16:41Speaker 2: They can be. The book shows using pythons built in
- 16:44Speaker 2: DateTime module along with the third party pits library to
- 16:47Speaker 2: handle time zone conversions correctly, like displaying a time recorded
- 16:52Speaker 2: in UTC for someone in say Pacific time.
- 16:54Speaker 1: What about displaying characters from other languages like German, Lauts
- 16:59Speaker 1: or maybe Asians.
- 17:00Speaker 2: Unicode handling Python three is generally pretty good with Unicode strings,
- 17:03Speaker 2: but sometimes, depending on your operating system, terminal or IDE settings,
- 17:07Speaker 2: you might run into encoding issues where characters display incorrectly.
- 17:11Speaker 1: Any solutions mentioned using.
- 17:12Speaker 2: Python's direct Unicode escapes like U TOFC for you can
- 17:15Speaker 2: be a robust way, or more commonly, ensuring your source
- 17:18Speaker 2: code files are saved with UTF eight encoding and that
- 17:21Speaker 2: your development environment like pit of eclip settings mentioned in
- 17:24Speaker 2: the book is configured to handle UTFA correctly.
- 17:27Speaker 1: Okay, building all this is great, but it needs to
- 17:29Speaker 1: work reliably. Testing. How does the book approach ensuring quality?
- 17:35Speaker 2: It emphasizes that testing isn't just one thing. It happens
- 17:38Speaker 2: at multiple levels. There's developer testing just running the code.
- 17:41Speaker 2: Maybe the IDE catches syntax errors using source control like GIT,
- 17:46Speaker 2: which helps catch integration conflicts, API testing testing the non
- 17:50Speaker 2: GUI logic, integration testing, making sure different parts work together,
- 17:54Speaker 2: and finally end user testing.
- 17:56Speaker 1: All catch in different kinds of bugs.
- 17:58Speaker 2: Exactly, you need a mix for debugging when things go
- 18:01Speaker 2: wrong during development. Buggers, Yeah, using your IDEs debugger is
- 18:05Speaker 2: key setting break points to pause the code, inspecting the
- 18:08Speaker 2: values of variables at that point using watches stepping through
- 18:11Speaker 2: code line by line. The book notes that in GUIs
- 18:14Speaker 2: you often want to put break points inside your event
- 18:17Speaker 2: handler functions like button callbacks, because that's where the action
- 18:20Speaker 2: happens after the mainloop dispatches an event. It also suggests
- 18:24Speaker 2: setting up your own simple logging using Python's logging module.
- 18:28Speaker 2: May be configured in the if name equals main part
- 18:30Speaker 2: of your script.
- 18:31Speaker 1: Why custom logging.
- 18:32Speaker 2: It gives you more control than just print statements. You
- 18:35Speaker 2: can set different logging levels, debug info, warning, error, easily
- 18:39Speaker 2: switch between logging to the console or a file, and
- 18:42Speaker 2: filter messages. It's great for tracing what your application is doing,
- 18:46Speaker 2: especially in response to events.
- 18:47Speaker 1: And for more formal automated tests.
- 18:50Speaker 2: Python's built in unitist framework is the standard shown. The
- 18:54Speaker 2: idea is to write separate test code that imports your
- 18:57Speaker 2: application code and verifies its behavior.
- 19:00Speaker 1: Does that work?
- 19:00Speaker 2: You create test classes inheriting from unitist dot test case.
- 19:04Speaker 2: Inside you write methods whose names start with test. Within
- 19:07Speaker 2: these methods, you call parts of your application code and
- 19:10Speaker 2: use assertion methods like self dot assert equal actual expected
- 19:14Speaker 2: to check if the results are correct.
- 19:16Speaker 1: But how do you test gy interactions with unitist. You
- 19:19Speaker 1: can't easily click a button in a test script.
- 19:21Speaker 2: Right, You usually don't simulate the physical click. Instead, you
- 19:25Speaker 2: test the consequences of the click. The book gives an example.
- 19:28Speaker 2: You directly call the callback function associated with a radio
- 19:32Speaker 2: button in your test code. Then you check if, say
- 19:36Speaker 2: a label widget that should have been updated by that callback,
- 19:40Speaker 2: now contains the expected text. You're testing the logic triggered
- 19:44Speaker 2: by the event.
- 19:44Speaker 1: Ah testing the callbacks effect exactly.
- 19:47Speaker 2: The book also mentions test fixtures set up in tear
- 19:50Speaker 2: down methods, which let you run code to prepare the
- 19:53Speaker 2: environment before each test, like creating objects and clean up afterwards.
- 19:56Speaker 1: Okay, Ticker is built in and clearly powerful, but Python's
- 20:00Speaker 1: ecosystem is huge. Does the book look at any alternative
- 20:03Speaker 1: GUI libraries?
- 20:04Speaker 2: It does. It takes a look at wx Python, specifically
- 20:07Speaker 2: the Phoenix version, which works with modern Python. It's another
- 20:10Speaker 2: very popular mature library.
- 20:11Speaker 1: How's it different from Ticker, Well.
- 20:12Speaker 2: First, you have to install it separately. It's not built in,
- 20:15Speaker 2: often done using pipt af files. The basic structure feels
- 20:19Speaker 2: conceptually similar, though you initialize in wx dot app create
- 20:24Speaker 2: a main window, wx dot frame, add widgets, and start
- 20:28Speaker 2: a mainloof the book notes it has good documentation and
- 20:31Speaker 2: often provides widgets that look more native on different operating systems. Plus,
- 20:35Speaker 2: things like standard menu mars are easy to set up.
- 20:38Speaker 1: Can you mix them? Put a wx Python widget inside
- 20:41Speaker 1: a kinter window or vice versa.
- 20:43Speaker 2: The book explores this idea, but the conclusion seems to
- 20:47Speaker 2: be it's usually not a good idea. Trying to embed
- 20:50Speaker 2: one framework directly inside another often leads to weird rendering issues, crashes,
- 20:55Speaker 2: or problems shutting down properly.
- 20:56Speaker 1: So what if you need parts built with different.
- 20:58Speaker 2: Toolkits, Launching them is completely separate windows? Maybe even separate
- 21:01Speaker 2: processes seems more stable?
- 21:03Speaker 1: And how would those separate windows talk to each other?
- 21:06Speaker 2: Back to our friend, the queue que shared cues are
- 21:09Speaker 2: presented again as a robust Pythonic way for independent GOI processes,
- 21:13Speaker 2: even if built with different frameworks, to pass messages or
- 21:16Speaker 2: data back and forth.
- 21:17Speaker 1: Okay, cool? What about going beyond two D? Does it
- 21:20Speaker 1: touch on three D graphics?
- 21:22Speaker 2: Yep? It introduces libraries for creating true three D scenes,
- 21:26Speaker 2: primarily PIOPENNGL, which is Python's binding to the standard open
- 21:29Speaker 2: geographics library.
- 21:30Speaker 1: You'll need to install that and PILT.
- 21:32Speaker 2: Pilot is often used alongside or instead of piopen gl
- 21:36Speaker 2: for handling window creation, event loops, and drawing shapes in
- 21:39Speaker 2: three D more easily. The book shows an example of
- 21:42Speaker 2: creating a spinning colored three D cube.
- 21:45Speaker 1: So you can actually control the three dview with keyboard input.
- 21:48Speaker 2: Yeah, you can capture keypresses within the pilot open gl
- 21:51Speaker 2: window and use them to trigger actions like rotating the
- 21:54Speaker 2: camera or the three D object.
- 21:56Speaker 1: Can these three D libraries create their own windows without
- 21:59Speaker 1: needing to kin wx.
- 22:01Speaker 2: Python, Yes, Absolutely, Pilot or even lower level open gl
- 22:05Speaker 2: utility libraries like glut can manage their own windows and
- 22:08Speaker 2: event loops, specifically for rendering the three D graphics and
- 22:11Speaker 2: handling input for that window. The book even shows simple
- 22:13Speaker 2: animation examples.
- 22:15Speaker 1: Okay, one quick step back to two D basics images
- 22:17Speaker 1: is to kiner good with images out of the box.
- 22:19Speaker 2: Uh not really. The built in kinter photo image is
- 22:23Speaker 2: surprisingly limited. It handles formats like GIF and PGMPPM, but
- 22:27Speaker 2: famously not common ones like JPEG or PNG.
- 22:30Speaker 1: Previously no JPEGs, nope to.
- 22:33Speaker 2: Display JPEGs, p and g S and other modern formats
- 22:36Speaker 2: in kinter, you pretty much need the Pillow library. It's
- 22:38Speaker 2: the modern successor to the old Python Imaging library PIL.
- 22:42Speaker 1: So you install pillow right PIP and sall pillow.
- 22:45Speaker 2: Then you use image tk dot photo image from the
- 22:47Speaker 2: PIL module. Yes, the import is still often PIL to
- 22:51Speaker 2: load your image file and that object can be used
- 22:53Speaker 2: in Kinder labels or buttons.
- 22:55Speaker 1: Got it. So Pillow is essential for real world image
- 22:57Speaker 1: handling and teeter that covers a mass of amount of ground,
- 23:01Speaker 1: from basic widgets to three D and databases. How does
- 23:04Speaker 1: the book tie this all together with advice on writing
- 23:07Speaker 1: good gy code best practices.
- 23:09Speaker 2: It definitely emphasizes writing code that's clean, maintainable, and extendable.
- 23:13Speaker 2: A big theme is avoiding spaghetti code. Spaghetti code, yeah,
- 23:16Speaker 2: you know, code that's all tangled up, jumps around unpredictably
- 23:18Speaker 2: with globals or complex logic flows, impossible to understand or
- 23:22Speaker 2: modify without breaking something. The opposite is modular, well structured code.
- 23:26Speaker 1: How do you achieve that structure in a bigger project
- 23:29Speaker 1: with multiple files?
- 23:30Speaker 2: The book mentions the role of in it dot pi
- 23:33Speaker 2: files in making directories into Python packages. It even shows
- 23:36Speaker 2: some techniques using CIS dot path or site dot ad,
- 23:40Speaker 2: site deer with an in dot PII to help Python
- 23:43Speaker 2: find your modules correctly, especially if you're running scripts from
- 23:46Speaker 2: different locations outside of IT, and the procedural versus OP
- 23:50Speaker 2: discussion comes back. The strong recommendation is for anything beyond
- 23:53Speaker 2: a very simple script, use object oriented programming. It just
- 23:57Speaker 2: scales better and makes maintenance feasible. Procedural is okay for
- 24:00Speaker 2: tiny utilities.
- 24:01Speaker 1: What about naming things, files, variables.
- 24:03Speaker 2: Functions meaningful names? This is stressed. Name your modules based
- 24:08Speaker 2: on what they do, like guywidgets dot PI or database
- 24:11Speaker 2: connector dot pi, not mod one dot PI or utilsnew
- 24:15Speaker 2: dot PI. Same for variables and functions. Make them descriptive,
- 24:19Speaker 2: avoid cryptic abbreviations. Consistency helps hugely, especially if you're working
- 24:24Speaker 2: in a team, but even just for your future self.
- 24:27Speaker 2: Design patterns also get a brief mention, like the factory
- 24:30Speaker 2: pattern as a way to handle more complex structural problems
- 24:34Speaker 2: like creating different variations of widgets systematically. It's more of
- 24:37Speaker 2: an introduction to the idea that these patterns exist to solve.
- 24:40Speaker 1: Common problems and just generally managing the complexity as you
- 24:43Speaker 1: add more and more features.
- 24:45Speaker 2: Yeah, it acknowledges GUIs naturally get complex. Practical tips include
- 24:49Speaker 2: using label frame containers to visually group related controls, using
- 24:53Speaker 2: loops to create multiple similar widgets instead of copy pasting code,
- 24:56Speaker 2: and organizing really large UIs into logical sections using TT
- 25:00Speaker 2: notebook tabs. These you are always to break down the complexity.
- 25:03Speaker 1: Wow. Okay, that really was a deep dive into this cookbook.
- 25:05Speaker 1: We've gone from literally the first import kinter and main
- 25:08Speaker 1: loop right.
- 25:09Speaker 2: Up to placing widgets, arranging them with grid sticky label frames.
- 25:13Speaker 1: Handling user input with string bars and callbacks, making things
- 25:17Speaker 1: responsive with threads and cues.
- 25:19Speaker 2: Connecting to my SQL databases, thinking about different languages with
- 25:23Speaker 2: I eighteen N.
- 25:24Speaker 1: And L ten N, testing with unit test, debugging, exploring
- 25:29Speaker 1: alternatives like wx Python, even touching on three D with
- 25:33Speaker 1: PIOPENNGL and PIGLT, and.
- 25:35Speaker 2: Finally wrapping up with best practices like OOP and good
- 25:39Speaker 2: naming to avoid that spaghetti code.
- 25:41Speaker 1: This book seems to lay out a really comprehensive path
- 25:43Speaker 1: full of practical examples. It shows Python's power, but also
- 25:46Speaker 1: points out where you need extra libraries like pillow or pits.
- 25:50Speaker 2: Yeah, it gives a solid map of the territory for
- 25:52Speaker 2: building desktop applications with Python. GUIs covering a lot of
- 25:56Speaker 2: the common challenges you'd encounter.
- 25:57Speaker 1: So having surveyed this whole landscape, from the cops button
- 26:00Speaker 1: to networked, database driven apps and even three D, it
- 26:04Speaker 1: makes you wonder, for you listening, what kind of GUI challenge,
- 26:07Speaker 1: maybe something you've seen elsewhere or thought about building, are
- 26:09Speaker 1: you now most curious to try and tackle using Python