简体   繁体   English

Python - 带标签和照片的Tkinter

[英]Python - Tkinter with labels & photos

I am making a program that gives you information and a picture about a planet/star. 我正在制作一个程序,为您提供有关行星/恒星的信息和图片。 I am using tkinter to do so. 我正在使用tkinter这样做。

Here is the code that the problem arises around: 以下是问题出现的代码:

sunPic = r'sun.gif'
mercPic = r'merc.gif'


buttonFrame = Frame(root)
buttonFrame.pack(side=LEFT)

textFrame = Frame(root)
textFrame.pack(side=TOP)


def sunInfo():

    sunImage = PhotoImage(file=sunPic)
    img1 = Label(textFrame, image = sunImage)
    img1.image = sunImage
    img1.pack()


def mercInfo():

    mercImage = PhotoImage(file=mercPic)
    img1.configure(image = mercImage)
    img1.image = mercImage
    img1.pack()

sun = Button(buttonFrame, text="THE SUN",command=sunInfo)
sun.pack(side=TOP)

mercury = Button(buttonFrame, text="MERCURY",command=mercInfo)
mercury.pack(side=TOP)

When you press a button, it is supposed to change images. 当您按下按钮时,它应该更改图像。 So say if I press THE SUN button, a pic of the sun would appear, and then I press the MERCURY button, a picture of mercury would replace the pic of the sun. 所以说如果我按下太阳按钮,会出现太阳的照片,然后我按下MERCURY按钮,水银图片将取代太阳的照片。

This doesn't work, however. 但是,这不起作用。 When I click on the mercury button it comes up with this error: 当我点击水银按钮时,会出现此错误:

img1.configure(image = mercImage)
NameError: global name 'img1' is not defined

Also, if I press THE SUN button several times, several images of the sun show up! 此外,如果我多次按下太阳按钮,会出现几张太阳图像! I do not want this. 我不想这样。

What I want to do in this program: 我想在这个程序中做什么:

  • Create buttons with the planets names on. 创建带有行星名称的按钮。

  • Allow the user to press those buttons 允许用户按下这些按钮

  • A PICTURE and some INFORMATION on that planet show up 图片和该星球上的一些信息显示出来

  • If the user presses another button, the PICTURE and INFORMATION is replaced by the new PLANETS information and picture. 如果用户按下另一个按钮,则PICTURE和INFORMATION将替换为新的PLANETS信息和图片。

This is a project I need to finish by tonight. 这是我今晚需要完成的一个项目。 I have little tkinter experience, and I need help. 我有很少的经验,我需要帮助。

Thank you. 谢谢。

ALSO: 也:

Here is the full code that I am working on: 这是我正在处理的完整代码:

from tkinter import *

root = Tk()



root.geometry('1024x768+200+200') # makes window (x,y+top left corner right + top left corner down)
root.title("Planetary Information") # creates title for window

sunPic = r'sun.gif'
mercPic = r'merc.gif'


buttonFrame = Frame(root)
buttonFrame.pack(side=LEFT)

textFrame = Frame(root)
textFrame.pack(side=TOP)


def sunInfo():

    sunImage = PhotoImage(file=sunPic)
    img1 = Label(textFrame)
    img1.configure(image = sunImage)
    img1.image = sunImage
    img1.pack()


def mercInfo():

    mercImage = PhotoImage(file=mercPic)
    img1.configure(image = mercImage)
    img1.image = mercImage
    img1.pack()




sun = Button(buttonFrame, text="THE SUN",command=sunInfo)
sun.pack(side=TOP)

mercury = Button(buttonFrame, text="MERCURY",command=mercInfo)
mercury.pack(side=TOP)

venus = Button(buttonFrame, text="VENUS")
venus.pack(side=TOP)

earth = Button(buttonFrame, text="EARTH")
earth.pack(side=TOP)

mars = Button(buttonFrame, text="MARS")
mars.pack(side=TOP)

jupiter = Button(buttonFrame, text="JUPITER")
jupiter.pack(side=TOP)

saturn = Button(buttonFrame, text="SATURN")
saturn.pack(side=TOP)

uranus = Button(buttonFrame, text="URANUS")
uranus.pack(side=TOP)

neptune = Button(buttonFrame, text="NEPTUNE")
neptune.pack(side=TOP)

root.mainloop() # 

It's not working because of the variable scope . 由于范围可变,它无法正常工作。 To put it simply: the variable img1 belongs to the function suninfo() , so when any other function tries to access it, it can't, so the traceback tells you: 简单地说:变量img1属于函数suninfo() ,所以当任何其他函数试图访问它时,它不能,所以回溯告诉你:

NameError: global name 'img1' is not defined

The best fix would be to put your code inside a class, but the quickest fix would be to make img1 outside the function, then update it as required: 最好的解决方法是将您的代码放在一个类中,但最快的解决方法是在函数外部创建img1 ,然后根据需要更新它:

buttonFrame = Frame(root)
buttonFrame.pack(side=LEFT)

textFrame = Frame(root)
textFrame.pack(side=TOP)

img1 = Label(textFrame) #Create img1 outside the function
img1.pack()

def sunInfo():
    sunImage = PhotoImage(file=sunPic)
    img1.pack_forget()                 #Drop previous img1 picture
    img1.configure(image = sunImage)
    img1.image = sunImage
    img1.pack()                        #Re-pack as new image

def mercInfo():
    mercImage = PhotoImage(file=mercPic)
    img1.pack_forget()
    img1.configure(image = mercImage)
    img1.image = mercImage
    img1.pack()

sun = Button(buttonFrame, text="THE SUN",command=sunInfo)
sun.pack(side=TOP)

mercury = Button(buttonFrame, text="MERCURY",command=mercInfo)
mercury.pack(side=TOP)

The reason the picture isn't changing is because you need to update the widget. 图片未更改的原因是您需要更新窗口小部件。 This can also be accomplished by the .place() method. 这也可以通过.place()方法完成。 Place the images on the same coordinates and it will change. 将图像放在相同的坐标上,它会改变。

The reason the sun picture is showing several times is because of the .pack() method. 太阳图片多次显示的原因是因为.pack()方法。 The pack method packs the widget, automatically. pack方法自动打包小部件。 You need to either create a update function or use the .place() method as said above. 您需要创建更新函数或使用上述的.place()方法。

Finally, when you press the mercury button, img1 isn't defined because it is not defined. 最后,当您按下水银按钮时,未定义img1,因为它未定义。 You can create a global and put the global in the sun function and the mercury function. 您可以创建global并将全局置于sun函数和mercury函数中。

The easiest way to refresh widgets is using the .grid() method and when you want to refresh the widget, use .grid_forget() . 刷新窗口小部件的最简单方法是使用.grid()方法,当您想要刷新窗口小部件时,请使用.grid_forget()

You should read up on this and learn about the widgets you use. 你应该在阅读了 ,了解你使用的小工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM