【问题标题】:splash (loading image) center of screen before main starts在主启动之前启动屏幕中心(加载图像)
【发布时间】:2021-10-29 20:23:55
【问题描述】:

我有一个图像 (400x400),我希望它作为启动图像(在主程序/代码启动之前)位于我的屏幕中心 (1920x1080)。我的问题是我的启动图像周围出现黑屏/窗口。我的代码:

import tkinter as tk
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="test.gif"
image = tk.PhotoImage(file=image_file)
canvas = tk.Canvas(root, height=height*0.8, width=width*0.8, bg='black')
canvas.create_image(width*0.8/2, height*0.8/2, image=image)
canvas.pack()

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

我的启动画面周围出现黑屏

这是我的初始图片

谢谢各位!

【问题讨论】:

    标签: python tkinter splash-screen


    【解决方案1】:

    这是我的最终答案,获取图像大小,然后使窗口与图像一样长和宽,然后将窗口放在中心,这确实包括一些简单的计算:

    from tkinter import *
    
    root     = Tk()
    img_file = "test.gif"
    image    = PhotoImage(file=img_file)
    w,h      = image.width(), image.height()
    
    screen_width  = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    
    x = (screen_width  / 2) - (w / 2)
    y = (screen_height / 2) - (h / 2)
    
    root.overrideredirect(True)
    root.geometry(f'{w}x{h}+{int(x)}+{int(y)}')
    
    canvas = Canvas(root, highlightthickness=0)
    canvas.create_image(0,0, image=image, anchor='nw')
    canvas.pack(expand=1,fill='both')
    
    root.after(5000, root.destroy)
    
    root.mainloop()
    

    使用root.eval('tk::PlaceWindow . center') 的问题在于它将窗口的左上角置于中心,因此整个应用程序本身并没有居中

    【讨论】:

      【解决方案2】:

      正如cool cloud所提到的,建议将您的画布精确设置为400x400的几何尺寸,但您可能还必须删除画布周围的白色高光。您可以通过以下方法实现:-

      canvas.config(highlightthickness=0)
      

      一旦你完成了,你就可以开始了,但需要额外的措施, 如果您仍然在启动画面中看到一些泄漏的画布,您可以执行以下操作:

      root.attributes('-transparentcolor',"lime")
      

      这将完全移除用户可见的画布。

      注意:- 为了使上述操作生效,您必须将画布的颜色设置为与图像中的任何颜色完全不同。 因为这种方法完全去除了窗口的颜色并使其透明

      希望这可行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-10
        • 2018-11-04
        • 2012-08-19
        • 1970-01-01
        • 2015-11-22
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        相关资源
        最近更新 更多