Jupyter脚本,带有图像,用于独立使用。

huangapple go评论73阅读模式
英文:

Jupyter script with images for standalone

问题

以下是您要翻译的代码部分:

import tkinter as tk
from PIL import Image, ImageTk

while True:
    name = input("\nWhat is your name? ")

    print(f'\nNice to meet you {name}')

    age = input("\nHow old are you? ")
    age = int(age)
    if age >= 40:
        # Open the "old" image
        image = Image.open(r'old.jpg')
    else:
        # Open the "young" image
        image = Image.open(r'young.jpg')

    # Convert the image to a PhotoImage object
    photo_image = ImageTk.PhotoImage(image)

    # Create a Tkinter window
    root = tk.Tk()

    # Create a label and set the image as its background
    label = tk.Label(root, image=photo_image)
    label.pack()

    # Run the Tkinter event loop
    root.mainloop()
    
    exit_prompt = input("\nEnter 'exit' to close the script, or press Enter to continue: ")
    if exit_prompt == "exit":
        break

请注意,我已经移除了代码中的HTML实体编码,以使其更加清晰。如果您需要任何进一步的帮助,请告诉我。

英文:

New to python created a very simple script but seem to be running into issues with images.
I want to take this script and create a standalone.
However when I did it originally without tkinter or imagetk the file would open and close immediately so I tried this.

You can see the error messages I get below the code.


> <sub>`import tkinter as tk from PIL import Image, ImageTk
> 
> while True:
>     name = input("\nWhat is your name? ")
> 
>     print(f'\nNice to meet you {name}')
> 
>     age = input("\nHow old are you? ")
>     age = int(age)
>     if age >= 40:
>         # Open the "old" image
>         image = Image.open(r'old.jpg')
>     else:
>         # Open the "young" image
>         image = Image.open(r'young.jpg')
> 
>     # Convert the image to a PhotoImage object
>     photo_image = ImageTk.PhotoImage(image)
> 
>     # Create a Tkinter window
>     root = tk.Tk()
> 
>     # Create a label and set the image as its background
>     label = tk.Label(root, image=photo_image)
>     label.pack()
> 
>     # Run the Tkinter event loop
>     root.mainloop()
>     
>     exit_prompt = input("\nEnter 'exit' to close the script, or press Enter to continue: ")
>     if exit_prompt == "exit":
>         break


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_986051054611.py in <module>
     17 
     18     # Convert the image to a PhotoImage object
---> 19     photo_image = ImageTk.PhotoImage(image)
     20 
     21     # Create a Tkinter window

~\anaconda3\lib\site-packages\PIL\ImageTk.py in __init__(self, image, size, **kw)
    138         self.__mode = mode
    139         self.__size = size
--> 140         self.__photo = tkinter.PhotoImage(**kw)
    141         self.tk = self.__photo.tk
    142         if image:

~\anaconda3\lib\tkinter\__init__.py in __init__(self, name, cnf, master, **kw)
   4062         Valid resource names: data, format, file, gamma, height, palette,
   4063         width."""
-> 4064         Image.__init__(self, 'photo', name, cnf, master, **kw)
   4065 
   4066     def blank(self):

~\anaconda3\lib\tkinter\__init__.py in __init__(self, imgtype, name, cnf, master, **kw)
   3995         self.name = None
   3996         if not master:
-> 3997             master = _get_default_root('create image')
   3998         self.tk = getattr(master, 'tk', master)
   3999         if not name:

~\anaconda3\lib\tkinter\__init__.py in _get_default_root(what)
    295     if not _default_root:
    296         if what:
--> 297             raise RuntimeError(f"Too early to {what}: no default root window")
    298         root = Tk()
    299         assert _default_root is root

RuntimeError: Too early to create image: no defa

ult root window`</sub>

答案1

得分: 0

I think you only need to call image.show() after image is opened if you want to show it.

英文:

I think you only need to call image.show() after image is opened if you want to show it.

from PIL import Image

while True:
    name = input(&quot;\nWhat is your name? &quot;)

    print(f&#39;\nNice to meet you {name}&#39;)

    age = input(&quot;\nHow old are you? &quot;)
    age = int(age)
    if age &gt;= 40:
        # Open the &quot;old&quot; image
        image = Image.open(r&#39;old.jpg&#39;)
    else:
        # Open the &quot;young&quot; image
        image = Image.open(r&#39;young.jpg&#39;)
    image.show()

答案2

得分: 0

以下是您要翻译的内容:

一旦这个问题被修复,我遇到了以下错误:

> --------------------------------------------------------------------------- TclError Traceback (most recent call
> last) ~\AppData\Local\Temp\ipykernel_11772\4142766009.py in <module>
> 26
> 27 # Create a label and set the image as its background
> ---> 28 label = tk.Label(root, image=photo_image)
> 29 label.image = photo_image # Keep a reference to the PhotoImage object
> 30 label.pack()
>
> ~\anaconda3\lib\tkinter_init_.py in init(self, master, cnf,
> **kw) 3146 3147 """
> -> 3148 Widget.init(self, master, 'label', cnf, kw) 3149 3150
>
> ~\anaconda3\lib\tkinter_init_.py in init(self, master,
> widgetName, cnf, kw, extra) 2570 for k, v in classes:
> 2571 del cnf[k]
> -> 2572 self.tk.call( 2573 (widgetName, self._w) + extra + self._options(cnf)) 2574 for k, v in
> classes:
>
> TclError: image "pyimage28" doesn't exist

我尝试通过存储图像来修复它:

> import tkinter as tk from tkinter import PhotoImage from PIL import
> Image, ImageTk
>
> while True:
> name = input("\nWhat is your name? ")
> print(f'\nNice to meet you {name}')
>
> age = input("\nHow old are you? ")
> age = int(age)
> if age >= 40:
> # Open the "old" image
> image = Image.open(r'old.jpg')
> else:
> # Open the "young" image
> image = Image.open(r'young.jpg')
>
> # Convert the image to a PhotoImage object
> photo_image = PhotoImage(image)
>
> # Create a Tkinter window
> root = tk.Tk()
>
> # Create a label and set the image as its background
> label = tk.Label(root, image=photo_image)
> label.image = photo_image # Keep a reference to the PhotoImage object
> label.pack()
>
> # Run the Tkinter event loop
> root.mainloop()
>
> exit_prompt = input("\nEnter 'exit' to close the script, or press Enter to continue: ")
> if exit_prompt == "exit":
> break

英文:

Once that issue was fixed I was getting the following error:

> --------------------------------------------------------------------------- TclError Traceback (most recent call
> last) ~\AppData\Local\Temp\ipykernel_11772\4142766009.py in <module>
> 26
> 27 # Create a label and set the image as its background
> ---> 28 label = tk.Label(root, image=photo_image)
> 29 label.image = photo_image # Keep a reference to the PhotoImage object
> 30 label.pack()
>
> ~\anaconda3\lib\tkinter_init_.py in init(self, master, cnf,
> **kw) 3146 3147 """
> -> 3148 Widget.init(self, master, 'label', cnf, kw) 3149 3150
>
> ~\anaconda3\lib\tkinter_init_.py in init(self, master,
> widgetName, cnf, kw, extra) 2570 for k, v in classes:
> 2571 del cnf[k]
> -> 2572 self.tk.call( 2573 (widgetName, self._w) + extra + self._options(cnf)) 2574 for k, v in
> classes:
>
> TclError: image "pyimage28" doesn't exist

I tried fixing it by storing the image:

> import tkinter as tk from tkinter import PhotoImage from PIL import
> Image, ImageTk
>
> while True:
> name = input("\nWhat is your name? ")
> print(f'\nNice to meet you {name}')
>
> age = input("\nHow old are you? ")
> age = int(age)
> if age >= 40:
> # Open the "old" image
> image = Image.open(r'old.jpg')
> else:
> # Open the "young" image
> image = Image.open(r'young.jpg')
>
> # Convert the image to a PhotoImage object
> photo_image = PhotoImage(image)
>
> # Create a Tkinter window
> root = tk.Tk()
>
> # Create a label and set the image as its background
> label = tk.Label(root, image=photo_image)
> label.image = photo_image # Keep a reference to the PhotoImage object
> label.pack()
>
> # Run the Tkinter event loop
> root.mainloop()
>
> exit_prompt = input("\nEnter 'exit' to close the script, or press Enter to continue: ")
> if exit_prompt == "exit":
> break

huangapple
  • 本文由 发表于 2023年1月6日 12:29:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定