### 基本图形用户界面程序实例 import tkinter as tk ##class FirstWin(tk.Frame): ## def __init__(self, title): ## tk.Frame.__init__(self) # 初始化 Frame 实例 ## # 属性 master 的值是本 Frame 对象所在的主窗口 ## self.master.title(title) # 设置窗口标题 ## self.pack() # 安置本Frame ## # 建立窗口中的文字对象,这是一个 Label 对象 ## self._label = tk.Label( ## self, # 第一个参数指定父物件 ## text = "下午好,这里是北京大学数学学院!") ## self._label.pack() # 设定Label的布局位置 ## self.mainloop() # 启动窗口的消息循环,接受消息并响应 ## ##FirstWin("My Window") ####### 显示一张图片 ##class ImageWin(tk.Frame): ## def __init__(self): ## tk.Frame.__init__(self) ## self.master.title("北京大学") ## self.pack() ## self._image = tk.PhotoImage(file = "pku.png") #图片对象 ## self._imageLabel = tk.Label( # 图片放入标签显示 ## self, image = self._image) ## self._imageLabel.pack() ## self._textLabel = tk.Label( ## self, text = "北京大学西门") ## self._textLabel.pack() ## self.mainloop() ## ##ImageWin() ###### 下面例子来自标准库手册,稍微修改 ##class Application(tk.Frame): ## def __init__(self): ## tk.Frame.__init__(self) ## self.master.title("Main") ## self.pack() ## self.createWidgets() # 调用下面方法设置窗口内的物件 ## self.mainloop() ## ## def createWidgets(self): # 在窗口里设置两个按钮 ## self._hi = tk.Button(self) # 采用分别设置属性的方式 ## self._hi["text"] = "Hello World\n(click me)" ## self._hi["command"] = self._say_hi # 关联命令(回调函数) ## self._hi.pack(side="top") # 位置在上 ## ## self._quit = tk.Button( # 创建时提供属性值 ## self, text="QUIT", fg="red", ## command=self.master.destroy) # 关联命令退出主窗口 ## self._quit.pack(side="bottom") # 位置在下 ## ## def _say_hi(self): ## print("hi there, everyone!") ## ##Application() ############### 讨论一段一般性情况 ################### #### 顺序排列的几个 Frame, #### 可设定边界宽度和类型 SUNKEN(凹陷), RAISED(突起), #### GROOVE, and RIDGE. ##class Frames(tk.Frame): ## def __init__(self): ## tk.Frame.__init__(self) ## self.master.title("Two Subframes") # 先布局两个子Frame ## self.pack() ## frame1 = tk.Frame(self, borderwidth=4, relief=tk.GROOVE) ## frame1.pack() ## frame2 = tk.Frame(self, borderwidth=4, relief=tk.RIDGE) ## frame2.pack() ## first = tk.Label(self, text="First label") ## # 文字在主Frame ## first.pack() ## second = tk.Label(frame1, text="Second label") ## second.pack() ## third = tk.Label(frame1, text="Third label") ## # 两段在子Frame ## third.pack() ## label4 = tk.Label( ## frame2, text="Fourth label") # 在另一个子Frame ## label4.pack() ## self.mainloop() ## ##Frames() ##### 按钮和操作,两个显示文字切换 ##### 展示运行中动态更改属性的操作 ##class ButtonWin(tk.Frame): ## def __init__(self): ## tk.Frame.__init__(self) ## self.master.title("Button Win") ## self.pack() ## # Lable, 设置文字的字体、字号和字形 ## self._label = tk.Label( ## self, text="Hello", ## font=('times', 24, 'bold')) ## self._label.pack() ## # Button, 设置字体、字号和字形 ## self._button = tk.Button( ## self, text = "Click me", ## font=('times', 24, 'bold'), ## command = self._switch) ## self._button.pack() ## ## self.mainloop() ## ## def _switch(self): # 同一个Label里的文字切换 ## if self._label["text"] == "Hello": ## self._label["text"] = "Goodbye" ## else: ## self._label["text"] = "Hello" ## ##ButtonWin() #### 输入框entry和变量label(用StringVar类的对象 var) #### 使用共享变量 var,实际输入立即反映到 Label 对象 ##class TextEntry(tk.Frame): ## def __init__(self): ## tk.Frame.__init__(self) ## self.master.title("Text Entry") # 文本输入框 ## self.pack() ## var = tk.StringVar() # 创建字符容器变量实例 ## # 下面 Label 和 Entry 共享一个字符容器对象 var ## label = tk.Label( ## self, textvariable=var, ## font=('times', 24, 'bold')) ## label.pack() ## entry = tk.Entry( ## self, textvariable=var, ## font=('times', 24, 'bold')) ## entry.pack() ## ##TextEntry().mainloop() #### 文本框,通过按钮向文本框加入内容 ##class Inserter(tk.Frame): ## def __init__(self): ## tk.Frame.__init__(self) ## self.master.title("Counter") # 文本输入框 ## self.pack() ## self._text = tk.Text( ## self, height=5, width=10, ## font=('Courier', 24, 'bold')) ## self._text.pack() ## button = tk.Button( ## self, text='Add', command=self._cross) ## button.pack() ## ## def _cross(self): # INSERT表示光标位置 ## self._text.insert(tk.INSERT, 'X') ## ##Inserter().mainloop() #### 实现一个点击记数器,IntVar计数,在label显示 #### 用 grid 布局器,可以指定行列位置 ##class Counter(tk.Frame): ## def __init__(self): ## tk.Frame.__init__(self) ## self.master.title("Counter") # 文本输入框 ## self.pack() ## ## self._counter = tk.IntVar() # 计数变量 ## self._counter.set(0) # 初值设0 ## # 两个按钮,分别向上和向下计数 ## button1 = tk.Button( ## self, text='Up', font=('times', 24, 'bold'), ## command=self._click_up, width=5) ## button1.grid(row=0, column=0) ## button2 = tk.Button( ## self, text='Down', font=('times', 24, 'bold'), ## command=self._click_down, width=5) ## button2.grid(row=0, column=2) ## # 一个标签, 显示计数值 ## label = tk.Label( ## self, font=('times', 24, 'bold'), ## textvariable=self._counter, width=5) ## label.grid(row=1, column=1) ## ## # 两个方法,分别实现加一和减一命令 ## def _click_up(self): ## self._counter.set(self._counter.get() + 1) ## def _click_down(self): ## self._counter.set(self._counter.get() - 1) ## ##Counter().mainloop() ###### 输入和计算圆盘面积 import math class CircleAreaCalculate(tk.Frame): def __init__(self): tk.Frame.__init__(self) self.master.title("Circle Area Calculation") self.grid() # 有关半径的标签和变量 self._radiusLabel = tk.Label(self, text = "Radius", font=('Courier', 24, 'bold')) self._radiusLabel.grid(row = 0, column = 0) self._radiusVar = tk.DoubleVar() self._radiusEntry = tk.Entry( self, textvariable = self._radiusVar, font=('Courier', 24, 'bold')) self._radiusEntry.grid(row = 0, column = 1) # 有关面积的标签和变量 self._areaLabel = tk.Label(self, text = "Area", font=('Courier', 24, 'bold')) self._areaLabel.grid(row = 1, column = 0) self._areaVar = tk.DoubleVar() self._areaEntry = tk.Entry( self, textvariable = self._areaVar, font=('Courier', 24, 'bold')) self._areaEntry.grid(row = 1, column = 1) # 计算命令按钮 self._button = tk.Button( self, text = "Compute", command = self._area, font=('Courier', 24, 'bold')) self._button.grid(row = 2, column = 0, columnspan = 2) self.mainloop() def _area(self): # 面积计算函数 try: radius = self._radiusVar.get() # 转换可能出错 if radius < 0.0: raise ValueError area = radius ** 2 * math.pi self._areaVar.set(area) except ValueError: # 数据类型或值错时弹出错误窗口 tk.messagebox.showerror( title="Circle Area", message = "Error: Bad Number", parent = self) CircleAreaCalculate() #### 创建文件菜单 ##class WinMenus(tk.Frame): ## def __init__(self): ## tk.Frame.__init__(self) ## self.master.title("Circle Area Calculation") ## self.master.geometry("640x480") # 设置主窗口大小 ## self.grid() ## ## self._text = tk.Text(self, height=10, width=80) ## self._text.pack() ## ## menubar = tk.Menu(self) ## filemenu = tk.Menu(menubar) ## filemenu.add_command(label='Save', #用lambda传递对象数据属性 ## command=lambda : self._save(self._text)) ## filemenu.add_command(label='Quit', command=self._quit) ## helpmenu = tk.Menu(menubar) ## ## menubar.add_cascade(label = 'File', menu=filemenu) ## menubar.add_cascade(label = 'Help', menu=helpmenu) ## self.master.config(menu=menubar) ## self.mainloop() ## ## def _save(self, text): ## data = text.get('0.0', tk.END) # 0.0 表示文本开始的行列位置 ## filename = tk.filedialog.asksaveasfilename( # 弹出文件选择框 ## parent=self, title='Save as...', ## filetypes=[('Text', '*.txt')]) ## try: ## with open(filename, 'w') as w: ## w.write(data) ## except: ## tk.messagebox.showerror( # 弹出错误消息框 ## title="Write File", parent = self, ## message = "Error: Can't open: {}".format(filename)) ## ## def _quit(self): ## self.master.destroy() ## ##WinMenus()