关于python GUI编程(Tkinter) 创建子窗口及在窗口上用图片绘图

  注意主窗口一定要为tk.Tk(),在主窗口上通过button的点击相应子函数创建子窗口,注意此时创建出来的窗口必须是Toplevel,否则出错。至于用图片在窗口上绘图,则按代码所示即可。



# -*- coding: utf-8 -*-

"""
Created on Wed Oct 26 20:32:52 2016


@author: min
"""


import Tkinter as tk
from PIL import Image, ImageTk 
global attackTime
attackTime=1


def show1():
    top1=tk.Toplevel()
    image = Image.open('random.jpg') 
    img = ImageTk.PhotoImage(image)
    canvas1 = tk.Canvas(top1, width = image.width*2 ,height = image.height*2, bg = 'white')
    canvas1.create_image(0,0,image = img,anchor="nw")
    canvas1.create_image(image.width,0,image = img,anchor="nw")
    canvas1.pack()   
    top1.mainloop()


def show2():
    top1=tk.Toplevel()
    image = Image.open('random.jpg') 
    img = ImageTk.PhotoImage(image)
    canvas = tk.Canvas(top1, width = image.width ,height = image.height, bg = 'white')
    canvas.create_image(0,0,image = img,anchor="nw")
    canvas.pack()   
    top1.mainloop()


def showMessage():
    top=tk.Toplevel()
    l=tk.Label(top,text='Attacks cost '+str(attackTime)+' s',width=20)
    l.pack()
    top.mainloop()
    
root=tk.Tk()
b1=tk.Button(root,text='start1',command=show1)
b1.pack()
b2=tk.Button(root,text='start2',command=showMessage)
b2.pack()

root.mainloop()









你可能感兴趣的:(python,python,gui,tkinter创建子窗口,绘图,minmin很潇洒)