【问题标题】:Image display on pop up window弹出窗口中的图像显示
【发布时间】:2022-10-04 18:53:47
【问题描述】:

我正在使用 pywebview 开发 Windows 桌面应用程序。我想在启动窗口上获得完整的图像 5 秒。一个非常好的示例 Easeus 应用程序在启动时会显示这样的图像;

这些是我的python代码;

import webview
import requests
import tkinter as tk

# initializing URL
url = "http:127.0.0.1:81"
timeout = 10
try:
# requesting URL
request = requests.get(url,
                       timeout=timeout)
webview.create_window('Quiximusic', 'http://127.0.0.1:8000/', resizable=True)
webview.start()

# catching exception
except (requests.ConnectionError,
    requests.Timeout) as exception:
window = tk.Tk()
greeting = tk.Label(text="Hello, Tkinter")
greeting.pack()

【问题讨论】:

  • 我不知道webview - 但如果你想显示带有tkintertk.Label( image=...) 的弹出窗口,那么你可以使用window.after(5000, window.destroy) 并在5000 毫秒(5 秒)后运行window.destroy() 并关闭窗口.
  • 我不知道webview,但如果它运行HTML,那么它可能需要JavaScript 重新加载或关闭网页。
  • pywebview 的官方文档中,我找到了示例destroy window - 它会在 5 秒后关闭窗口。

标签: python pywebview


【解决方案1】:

pywebview 的官方文档中,我找到了示例destroy window。它会在 5 秒后关闭窗口。

import webview
import time

def destroy(window):
    # show the window for a few seconds before destroying it:
    time.sleep(5)
    print('Destroying window..')
    window.destroy()
    print('Destroyed!')

if __name__ == '__main__':
    window = webview.create_window('Destroy Window Example', 'https://pywebview.flowrl.com/hello')
    webview.start(destroy, window)
    print('Window is destroyed')

但是如果您使用tkinter,那么您可以使用tk.Label(image=...) - 和window.after(5000, window.destroy) 在5000 毫秒(5 秒)后关闭窗口

import tkinter as tk

window = tk.Tk()

img = tk.PhotoImage(file='image.png')  # has to be `file=`

tk.Label(image=img).pack()

window.after(5000, window.destroy)     # `destroy` without `()`

window.mainloop()

对于.jpg,它可能需要PIL.ImageTk

import tkinter as tk
from PIL import ImageTk

window = tk.Tk()

img = ImageTk.PhotoImage(file='image.jpg')  # has to be `file=`

tk.Label(image=img).pack()

window.after(5000, window.destroy)     # `destroy` without `()`

window.mainloop()

编辑:

您还可以使用 pywebview 显示带有图像的 HTML,并带有标签 <meta http-equiv="refresh" content="5;https://...">,它会在 5 秒后重定向到其他页面

索引.html

<meta http-equiv="refresh" content="5;https://stackoverflow.com">

<a href="https://en.wikipedia.org/wiki/Lenna">Lenna</a> from Wikipedia:<br>

<img src="https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png">

主文件

import webview

webview.create_window('Example', 'index.html')
webview.start()

【讨论】:

  • 它工作得很好,但现在我希望它在图像显示 5 秒后显示我的网站
  • 关闭图像后使用您的页面运行webview。如果您使用webview 显示图像,那么您必须在destroy() 中执行此操作。如果您使用tkinter 显示图像,那么您必须在window.mainloop() 之后执行此操作。最终,您可以使用webview 显示带有图像和 JavaScript 代码的网页,该代码在 5 秒后更改 document.location = 'http:// ...(使用 setTimer()) - 它可以加载带有应用程序的页面。但是这种方式即使在普通的网络浏览器中也可以工作,并且不需要 Python。
  • 在网页上,您还可以使用标签&lt;meta http-equiv="refresh" content="5;url_to_new_page"&gt; 在 5 秒后重新加载其他页面。 HTML meta http-equiv Attribute
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 2021-04-26
相关资源
最近更新 更多