SoFunction
Updated on 2024-11-14

Detailed knowledge of tkinter in python3

#Import the tkinter module, and import the ttk module, tkinter is the standard interface for python with tk, ttk is the "theming toolkit" added after TK8.5.
from tkinter import *
from tkinter import ttk

#Defined calculation functions to complete the conversion from feet to meters
def calculate(*args):
  try:
    value = float(())
    ((0.3048 * value * 10000.0 + 0.5)/10000.0)
  except ValueError:
    pass

'''
A main window is created;
The main window was titled: "Free to Meters"
By creating a frame container; so the interface content is placed in the frame and the frame is placed in the main window root;
The grid specifies the position of the frame, and the alignment: sticky tells how the controls will be placed in the grid cells (NWES for top, left, right, and bottom, respectively), using a compass orientation;
The columnconfigure method tells tk to adapt the width;
The rowconfigure method tells tk to adapt the height.
'''
root = Tk()
("Feet to Meters")
mainframe = (root, padding="3 3 12 12")
(column=0, row=0, sticky=(N, W, E, S))
(0, weight=1)
(0, weight=1)

feet = StringVar()
meters = StringVar()

feet_entry = (mainframe, width=7, textvariable=feet)
feet_entry.grid(column=2, row=1, sticky=(W, E))

(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)

(mainframe, text="feet").grid(column=3, row=1, sticky=W)
(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
(mainframe, text="meters").grid(column=3, row=2, sticky=W)

'''
Check all the controls in the frame and add a little distance between each one so they don't look so crowded;
Tell tk to turn its attention to our input box; that is, at the beginning, the cursor will be in the input box by default, and there's no need to click on it while the user is typing
Tell tk that the user pressing the Enter key is the same as clicking the Calculate button, and will call the Calculate program segment
'''
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
feet_entry.focus()
('<Return>', calculate)

#tk makes his events loop so that they all run
()

Forgot something?

Here are a few more worth checking out that we didn't include in the Tk program. For example:

  • -We didn't consider redrawing the interface when the event changed
  • -We did not consider evaluating sending event messages, capturing monitoring, or handling events on a per-space basis
  • -When we created the control, we didn't provide any more options; the default seems to have focused on a lot of things, and we've only changed the display text on the buttons.
  • -We didn't write complicated code to get and set the value of the simple controls, we just linked them to the variable
  • -We didn't consider what happens when the user closes the window or changes the window size
  • -We didn't write extra code to make it work across platforms