python GUI编程(Tkinter) 创建子窗口及在窗口上用图片绘图实例
作者:minmin很潇洒 时间:2021-12-21 19:37:29
注意主窗口一定要为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 tkinter中出现的坑(界面Tk()+图片显示)
一、关于Python3的tkinter模块
1、首先关于创建Python的窗口是导入 import tkinter 或者 from tkinter import * 这两种形式。关于创建tkinter 的大家耳熟能详的就是直接 win=Tk()[在导入方式为from tkinter import *形式下],但是还有另一种方法用来创建窗口那就是:win=Toplevel(),这个代表的是创建二级界面,就是直接创建两个界面,这个方法非常实用,应用在多个函数调用并生成Python窗口上面。小逸亲自尝试了一下,相当的好哈哈~~~~
2、Toplevel()实际操作。
首先,我们在Python3的环境下写下以下简单的代码:
from tkinter import *
win=Toplevel()
win.title=("这是一个二级界面")
win.geometry("500x300+10+10")
win.mainloop()
上面的代码运行后将出现以下的两个窗口:
二、# 关于在Label中显示图片的大坑
1、在Label 中显示图片需要用到tkinter 与pillow这两个模块
单独运行一个在tkinter上显示的图片没有问题,但是如果把这个显示图片的函数放在一个Button的command中,那么就算用二级界面也不行了,这个是一个非常大的坑,但是解决方法也非常非常的简单。只要将处理图片的两行代码放在外面就行了。如图:
来源:https://blog.csdn.net/min954584739/article/details/56842950
标签:python,Tkinter,子窗口,绘图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python连接SQLServer2000的方法详解
2023-07-23 09:02:12
发一个自己用的JS框架(试用版^_^)
2011-09-11 18:53:24
python获取当前计算机cpu数量的方法
2023-08-01 14:20:03
认清区别CSS的类class和id
2007-10-08 12:02:00
用面包屑来简化多层的tabs
2011-06-16 20:32:54
IE9初窥:支持CSS3,和HTML5?
2009-12-01 14:20:00
![](https://img.aspxhome.com/file/UploadPic/200912/1/dean_pdc_5-27s.png)
Python根据指定文件生成XML的方法
2022-09-06 21:00:10
![](https://img.aspxhome.com/file/2023/7/76687_0s.png)
python中学习K-Means和图片压缩
2022-08-15 14:09:07
![](https://img.aspxhome.com/file/2023/3/76753_0s.png)
ASP checkbox复选框是否被选中的代码(结合数据库)
2011-03-06 11:21:00
Python time模块之时间戳与结构化时间的使用
2024-01-02 09:07:51
![](https://img.aspxhome.com/file/2023/3/66383_0s.png)
解决在pycharm中显示额外的 figure 窗口问题
2021-11-29 01:00:26
![](https://img.aspxhome.com/file/2023/3/75383_0s.jpg)
WEB标准与XHTML 1.0 Transitional等文档类型介绍
2007-10-20 21:18:00
如何用 Python 子进程关闭 Excel 自动化中的弹窗
2023-10-16 15:47:32
![](https://img.aspxhome.com/file/2023/2/71452_0s.png)
Python Django模板之模板过滤器与自定义模板过滤器示例
2023-10-31 20:37:48
如何将txt文本中的数据轻松导入MySQL表中
2009-03-06 17:35:00
MySQL的一些安全注意点
2008-12-24 16:29:00
PHP取整函数:ceil,floor,round,intval的区别详细解析
2023-11-17 18:30:33
解决phpmyadmin中缺少mysqli扩展问题的方法
2023-07-13 02:30:12
简单代码屏蔽超级链接虚线框
2008-02-03 11:34:00
python中强制关闭线程与协程与进程方法
2023-05-11 10:24:48
![](https://img.aspxhome.com/file/2023/4/65074_0s.png)