这两天玩了一下tkinter,学习了一些控件及布局,和Qt界面编程比起来真的好吃力,下面以代码的形式记录一下作一小结,也可以当作以后用tkinter构建GUI的初级模板
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 8 14:09:49 2018
@author: 周文青
Contruct a template for tkinter
"""
import tkinter as tk
from tkinter import Menu, LabelFrame
from tkinter import ttk
from tkinter.ttk import Label, Entry, Button, Combobox, Checkbutton, Radiobutton
#from tkinter.tix import *
from tkinter.scrolledtext import ScrolledText
from tkinter import messagebox as mBox
from tkinter.filedialog import * # 导入对话框相关包,可以打开文件浏览器、保存文件等操作
from tkinter.constants import RIGHT, LEFT, Y, BOTH, X, YES, NO
import pygame
from PIL import Image
from PIL.ImageTk import PhotoImage
#################窗口大小设置##################
win = tk.Tk()
win.title("Python GUI")
#################icon image##################
play = PhotoImage(Image.open("/home/kindy/Files/python/gui/tkinter/play.ico").resize((30,30)))
pause = PhotoImage(Image.open("/home/kindy/Files/python/gui/tkinter/pause.ico").resize((30,30)))
# 本机屏幕分辨率
screen_width = 1366
screen_height = 768
#win.minsize(50,50)
#win.maxsize(480,640)
win.geometry("640x420+363+144") # 使主窗口居中
############################################
#################创建工具栏##################
menuBar = Menu(win)
win.config(menu=menuBar)
## 添加File菜单
fileMenu = Menu(menuBar, tearoff=0) # 去除虚线效果
def _new():
'''
待完成回调函数
'''
pass
def _open():
'''
待完成回调函数
'''
filename = askopenfilename()
# add code here to do something with filename
def _save():
'''
待完成回调函数
'''
pass
def _saveas():
'''
待完成回调函数
'''
filename = asksaveasfilename()
# add code here to do something with filename
# 定义退出回调函数
def _exit():
'''
退出程序并且销毁
'''
win.quit()
win.destroy()
exit()
fileMenu.add_command(label="New", command=_new)
fileMenu.add_command(label="Open", command=_open)
fileMenu.add_command(label="Save", command=_save)
fileMenu.add_command(label="Save As", command=_saveas)
fileMenu.add_separator() # 添加分隔符
fileMenu.add_command(label="Exit", command=_exit) # 添加Exit选项,调用回调函数_quit
menuBar.add_cascade(label="File", menu=fileMenu) # 添加菜单名为File
## 添加Help菜单
helpMenu = Menu(menuBar, tearoff=0)
helpMenu.add_command(label="About")
menuBar.add_cascade(label="Help", menu=helpMenu) # 添加菜单名为Help
###########################################
###############创建新的window############
## newwinButton1 callback function
#def newwinButton1_callback():
# newwin1 = tk.Toplevel() # 新建一个newwin1的窗口
#
#newwinButton1 = Button(text="新建窗口1", command=newwinButton1_callback)
#newwinButton1.grid(column=0, row=0,)
##newwinButton1.pack(side=LEFT, expand=YES)
#
## newwinButton1 callback function
#def newwinButton2_callback():
# newwin2 = tk.Toplevel() # 新建一个newwin1的窗口
#
#newwinButton2 = Button(text="新建窗口2", command=newwinButton2_callback)
#newwinButton2.grid(column=1, row=0,)
##newwinButton2.pack(side=LEFT, expand=YES)
##############Open a .mp3 File#########
# Add a LabelFrame,管理音乐播放的相关控件
labelsFrame = LabelFrame(win)
labelsFrame.grid(row=1, columnspan=4, padx=10, pady=20) #使用padx和pady参数,
# Add a label
Label(labelsFrame,text="Select a MP3 File:").grid(row=10,column=0)
# an entry
name = tk.StringVar()
nameEntered = Entry(labelsFrame, width=25, textvariable=name)
nameEntered.grid(column=1, row=10)
#nameEntered.focus() #使用focus,使gui一运行光标就在Entry
#nameEntered.insert(tk.INSERT,"TEST")
# BROWSER Button callback
def browserButton_callback():
'''
按钮回调函数,返回文件名,并且显示在Entry上
'''
global filename
filename = askopenfilename(initialdir='/home/kindy/音乐',
filetypes=[('Music files', '.mp3'),('All files','*.*')])
nameEntered.insert(tk.INSERT, filename)
browserButton = Button(labelsFrame, text="Browse File", command=browserButton_callback)
browserButton.grid(column=2,row=10)
# playButton callback
global flag
flag = 0
def playButton_callback():
'''
音乐播放按钮回调函数,使用pygane播放音乐
'''
global filename
# playButton.configure(image=pause)
pygame.mixer.init()
pygame.mixer.music.load(filename)
pygame.mixer.music.play() # 播放音乐
playButton = Button(labelsFrame, text="Play>>", command=playButton_callback)
playButton.grid(column=3,row=10)
# stopButton callback
def stopButton_callback():
'''
音乐pause按钮回调函数
'''
global filename
# playButton.configure(image=pause)
pygame.mixer.init()
pygame.mixer.music.load(filename)
pygame.mixer.music.stop() # 停止播放
stopButton = Button(labelsFrame, text="Stop||", command=stopButton_callback)
stopButton.grid(column=4,row=10)
# 对labelsFrame下的控件布局进行优化
for child in labelsFrame.winfo_children():
child.grid_configure(padx=4, pady=4)
win.mainloop()
做出来的UI很原始哈,这里拿来现丑一下,好让有需要的伙伴心里有个底~
参考链接:
(1)http://www.cnblogs.com/chan7/p/5801953.html
(2)http://pillow.readthedocs.io/en/3.1.x/reference/ImageTk.html
(3)http://www.jb51.net/article/66301.htm