Py学习  »  Python

python 3 tkinter飞溅屏幕

Kitsune Makoto • 5 年前 • 1255 次点击  

我一直想在主程序出现之前添加一个显示为启动屏幕的图像,但是我无法让它工作。

我希望图像在监视器中居中,只显示图像而不显示其他内容。

我的代码是:

root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry('%dx%d+%d+%d' % (width*0.8, height*0.8, width*0.1, 
height*0.1))
image_file = "Flexbase.png"
image = tk.PhotoImage(file=image_file)
canvas = tk.Canvas(root, height=height*0.8, width=width*0.8, bg="brown")
canvas.create_image(width*0.8/2, height*0.8/2, image=image)
canvas.pack()
root.after(5000, root.destroy)
root.mainloop()

我做错什么了?有人能给我解释一下如何显示图像吗?

备注:如果这些信息有帮助的话,图片本身是640x160,没有任何透明的东西。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44914
 
1255 次点击  
文章 [ 1 ]  |  最新文章 5 年前
stovfl
Reply   •   1 楼
stovfl    6 年前

问题 :我希望图像在监视器中居中,只显示图像而不显示其他内容。

import tkinter as tk

root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenheight()

image_file = "Flexbase.png"
image = tk.PhotoImage(file=image_file)

  • 窗口 width height 应该等于 image 大小

    x = int((width / 2) - (image.width() / 2))
    y = int((height / 2) - (image.height() / 2))
    geometry = '{}x{}+{}+{}'.format(image.width(), image.height(), x, y)
    
    root.geometry(geometry)
    
  • ,进一步计算 canvas 尺寸 帆布 默认情况下展开。

    canvas = tk.Canvas(root)
    
  • 放置 形象 上/左 使用 anchor 上/左

    canvas.create_image(0,0, image=image, anchor='nw')
    

canvas.pack()

root.after(3000, root.destroy)
root.mainloop()

用python测试:3.5