做一些实用小工具。
1. tkinter
hello world
from tkinter import Tk, Label root = Tk() label = Label(root, text="hello world") label.pack() # pack() ⇒ 起到 Layout 布局的作用 root.mainloop()
按钮及其事件:
from tkinter import Tk, Frame, Label, Button, Entry from tkinter import DoubleVar class TempConverter: def __init__(self, master): mainframe = Frame(master) mainframe.pack() label1_1 = Label(mainframe, text='deg C') label1_1.grid(row=0, column=0) self.c_val = DoubleVar() entry1_2 = Entry(mainframe, textvariable=self.c_val) entry1_2.grid(row=0, column=1) self.f_val = DoubleVar() label2_1 = Label(mainframe, text='deg F') label2_1.grid(row=1, column=0) label2_2 = Label(mainframe, textvariable=self.f_val) label2_2.grid(row=1, column=1) button = Button(mainframe, text='Convert', command=self.convert) button.grid(row=2, columnspan=2) def convert(self): self.f_val.set(self.c_val.get() * 1.8 + 32) root = Tk() root.wm_title('tmp converter') converter = TempConverter(root) root.mainloop()
通过 grid() 成员的 row 和 column 属性进行布局,layout 如下: