Python

GUIs with tkinter

98 flashcards · answers and spaced-repetition review in the KnowCard app

Which GUI toolkit can you import and use without installing anything beyond a standard CPython install?
You build a full tkinter window — root, labels, buttons — and run the script. Nothing appears and the program exits instantly. What's missing?
What is wrong with this button? ``` def greet(): print("hi") b = tkinter.Button( root, text="Go", command=greet()) ```
In one tkinter container you call .pack() on one widget and .grid() on another. What happens?
You create a tkinter.Label(root, text="Hi") but never call .pack(), .grid(), or .place() on it. Does it show?
Name tkinter's three geometry managers and the one-line rule for choosing between them.
What does side do in widget.pack(side="left"), and what does a widget packed with side="left" occupy?
Why can't you just bind a tkinter Entry's text to a plain Python str variable to read/write it from code?
Match each tkinter control-variable class to the Python type it holds.
What does this print? ``` v = tkinter.IntVar() v.set("not a number") print("set ok") print(v.get()) ```
Two widgets share the same StringVar via textvariable. The user edits one. What happens to the other?
What's wrong with this attempt to print where the user clicked? ``` def on_click(): print(event.x, event.y) w.bind("<Button-1>", on_click) ```
A tkinter button's command= callback and an event handler bound with .bind("<Button-1>", ...) differ in one signature detail. Which?
What event string binds a left mouse-button click, and how do you also require the Shift key be held?
Inside a tkinter button callback you run a 10-second loop (e.g. downloading a file). What does the user see, and why?
When you read a multiline tkinter.Text widget with text.get("1.0", "end"), what does the index "1.0" mean?
You want a Text handler to read the text *including* the key the user just typed. Should you bind <KeyPress> or <KeyRelease>?
How do you give every Radiobutton in a group mutually-exclusive selection?
Wiring a Scrollbar to a Listbox needs two cross-connections. What are they?
For Canvas.create_arc, what does the style option choose, and what are the defaults for start and extent?
To make entries in a Qt list model editable, which method must the model implement, and what do items get by default if you don't?
When the user edits an entry, a Qt model's setData(index, value, role) writes the change. What must it do after updating the dataset, and why?
Qt, GTK, and wxWidgets are all written in C/C++. How does Python code end up driving them?
A custom Qt delegate's sizeHint(option, index) must return what, and what are its two parameters?
Inside a Qt delegate's paint(painter, option, index), where do you draw, and why should you bracket the drawing with painter.save() / painter.restore()?
Inside a Qt delegate's paint, how do you tell whether the entry you're drawing is the currently selected one?
For editing in a Qt delegate, which four methods handle creating the editor, filling it, sizing it, and saving it back?
Where is the origin of Qt's coordinate system, which way does the Y-axis point, and what is (0,0) relative to when you draw inside a widget?
bind and bind_all return a value when you register a handler. What is it, and when do you need it?
What does Listbox.curselection() return, and what's the easy bug when you use it?
You have a working PyQt program and want it to run under PySide6. Roughly how much work is that, and what's the catch?
wxPython/wxWidgets and Qt are both cross-platform. What does wxWidgets do differently to match each platform's look and feel?
tkinter apps commonly define a class that inherits from tkinter.Frame. What is a Frame, and why start there?
In pack, what's the difference between the expand and fill options when the window is resized?
In tkinter's packer, padx/pady and ipadx/ipady both add pixels of space. What's the difference?
You call .bind("<Button-1>", h2) on a widget that already had h1 bound to the same event. Does h1 still fire?
What event string binds a double-click of the left mouse button in tkinter?
In a tkinter event handler def h(event):, which attribute gives the typed character, and which gives the click position relative to the whole screen?
Your mouse-wheel handler bound to <MouseWheel> works on Windows but never fires on Linux. Why?
How do you query a tkinter widget's current on-screen position and size at runtime, and what format does winfo_geometry() return?
Name the two equivalent ways to set a tkinter widget's options, e.g. giving it width 200.
You add a tkinter Checkbutton but can't read whether it's checked. What did you forget to wire up?
Which tkinter widget draws a titled border around a group of child widgets, and how is the title set?
A user can only ever select one row in your tkinter Listbox, but you want multiple. Which option controls this?
You want code to run every time the user changes the selection in a tkinter Listbox. What do you bind?
How do you attach a menu bar to a tkinter root window, and why is tearoff=False usually passed to each Menu?
Which Menu methods add a clickable item, a divider, and a submenu, respectively?
A tkinter Menubutton and an OptionMenu both drop down a list on click. What's the key behavioral difference?
How do you construct a tkinter OptionMenu, and what argument does its command handler receive?
You wired a vertical scrollbar with yscrollcommand/yview. How do you make it horizontal, and can only a Listbox be scrolled this way?
A tkinter Spinbox normally spins through an integer range. How do you instead let the user step through an explicit set like the primes 2,3,5,7?
In a tkinter Text widget, how do you make certain inserted text appear orange or underlined?
Why can't a tkinter Text widget use a control variable like an Entry does, and how do you read its contents instead?
On a tkinter Canvas, where is the origin (0,0), and which way does the positive Y-axis point?
You call tkinter.PhotoImage(file="photo.jpg") to put an image on a Canvas and it fails. What's the limitation, and what does the anchor in create_image default to?
On a tkinter Canvas you call create_rectangle(10, 10, 50, 50). Which four numbers are those, and how does it differ from how most toolkits size a rectangle?
Where does Canvas.create_text(x, y, text="Hi") place the text relative to (x, y), and how do you specify its font?
You want to put an actual tkinter Button in the middle of a drawing on a Canvas. Which Canvas method does that, and what's the catch?
What kind of shape does Canvas.create_polygon produce, and can it handle a nonconvex outline?
The tkinter.scrolledtext.ScrolledText widget saves you writing boilerplate. What exactly does it bundle, and which scrollbar does it NOT give you?
Which tkinter.filedialog functions do you call to (a) pick a folder, (b) pick a file to open, and (c) pick a save location?
How can you discover, at runtime, the exact font family names tkinter will accept in a font=(name, size, style) tuple?
In tkinter.messagebox, what do askokcancel, askquestion, and askyesnocancel each return? (Note the odd ones out.)
Qt/PySide6 is free to download. When does using it actually cost you money?
The book insists Qt is a "framework," not just a "GUI toolkit." What does that distinction mean concretely?
Qt's central communication mechanism is "signals and slots." How do you actually wire a Qt button's click to your handler in PySide6?
A Qt signal fires with parameters (e.g. stateChanged(int)). What must the connected slot look like?
You designed a dialog in Qt Designer (a .ui file) and generated a Ui_MainDialog class. Why can't you just instantiate that class and show() it?
What three things does the boilerplate main block of every PySide6 app do, and how many QApplication objects should exist?
Why is GUI code called "event-driven," and what two techniques does Qt give you for handling events?
Reading the current value out of a Qt widget isn't uniform. Contrast how you read a QLineEdit vs a QDateEdit.
A Qt QCheckBox.checkState() can return more than the obvious two values. What are the three, and when would the third appear?
How do you populate a Qt QComboBox (dropdown) and read which entry the user picked?
A Qt QDateEdit lets the user pick a date via a calendar. How do you constrain and read the value, and which signal reports changes?
In Qt, what's the practical difference between showing a QDialog with exec() versus show()?
For single-line vs multiline text input in Qt, the getter/setter names differ. Give the pair for QLineEdit and for QTextEdit.
In tkinter you make Radiobuttons mutually exclusive by sharing a control variable. How does Qt group QRadioButtons instead, and how do you get two independent groups in one dialog?
Qt's QSlider and QDial differ in looks but share a data model. What value type do they produce and which methods set the range and read it?
A Qt QProgressBar differs from most widgets in one interaction respect. What is it, and how do you drive it?
Qt offers both QListWidget and QListView for showing a list. What's the architectural difference and when do you pick QListWidget?
In Qt, what makes a QWidget become a top-level window versus a child inside another widget?
To handle low-level events in Qt you subclass a widget and override methods. Name the handlers for redraw, resize, and a mouse click, and what each receives.
You override mouseMoveEvent in a Qt widget but it only fires while you drag with a button held - never on plain hover. Why, and how do you get hover motion?
To draw custom graphics in a Qt widget, which handler do you implement and how do you obtain the drawing tool inside it?
In Qt drawing, what's the division of labor between a QPen and a QBrush, and how many of each can be active at once?
What are Qt's QPen line-style constants, and which is the default?
What do the four arguments of Qt's painter.drawRect(x, y, w, h) mean, and which other shape methods share that exact signature?
Beyond rectangles, match these Qt painter methods to what they draw: drawArc, drawChord, drawPie, drawConvexPolygon, drawPolygon, drawLine.
How do you load an image file and paint it into a Qt widget, and what do the arguments of drawImage control?
The simplified painter.drawText(0, 0, "Hi") variant draws nothing at the top-left. Why, and what Y value do you actually need?
To place text within a rectangle in Qt you pass an alignment specifier to drawText. How do you center it both horizontally and vertically, and how is the font chosen?
Qt lets a QBrush fill with a gradient. What are the three gradient classes, and how does a gradient become a fill?
How do you make a Qt pen or brush semi-transparent (alpha blending), and what's the value range?
Your Qt drawing shows jagged, pixelated edges. Which single call smooths them, and what does it do?
If a Qt shape you need isn't a prefabricated primitive (rect, ellipse, etc.), what two facilities let you build and transform it?
What problem does Qt's model-view architecture solve, and must the model actually store the data?
You're writing a Qt list model by subclassing QAbstractListModel. Which two methods must you implement, and how do you know which element is being requested?
In Qt model-view, how do you attach a model to a view, and what's a "delegate" for?

Start learning today

Free to start — download the app or use it in your browser.

Get it on App StoreGet it on Google Play
GUIs with tkinter (Python) · KnowCard