I need help with the button action for this code. When you click on the button, a new window should open with the GIF image of the horoscope sign. The name of the GIF image files are "Pisces.gif", "Taurus.gif",etc.

from Tkinter import * #Call all modules for TKinter in order to make GUI.

app = Tk() #Define a variable for the TKinter interface.
app.title("Horoscope") #Apply GUI title
app.geometry("500x3000") #Size of window.

button1=Button(app, text="January 20-Febuary 18",width=20)
button1.pack(padx=15,pady=15)

button2=Button(app, text="February 19-March 20",width=20)
button2.pack(padx=15,pady=15)

button3=Button(app, text="March 21-April 19",width=20)
button3.pack(padx=15,pady=15)

button4=Button(app, text="April 20-May 20",width=20)
button4.pack(padx=15,pady=15)

button5=Button(app, text="May 21-June 20",width=20)
button5.pack(padx=15,pady=15)

button6=Button(app, text="June 21-July 22",width=20)
button6.pack(padx=15,pady=15)

button7=Button(app, text="July 23-August 22",width=20)
button7.pack(padx=15,pady=15)

button8=Button(app, text="August 23-September 22",width=20)
button8.pack(padx=15,pady=15)

button9=Button(app, text="September 23-October 22",width=20)
button9.pack(padx=15,pady=15)

button10=Button(app, text="October 23-November 21",width=20)
button10.pack(padx=15,pady=15)

button11=Button(app, text="November 22-December 21",width=20)
button11.pack(padx=15,pady=15)

button12=Button(app, text="December 22-January 19",width=20)
button12.pack(padx=15,pady=15)

app.mainloop() #Final input for making GUI

Dani AI

Generated

Good progress so far: provided the button list, demonstrated opening an image in a child window, and suggested generating buttons in a loop. The following clarifications and practical tips address common pitfalls not spelled out in the replies and make the approach more robust across platforms and Python versions.

Photo/image handling — PhotoImage objects can disappear if not kept referenced; attaching the PhotoImage to the Toplevel (for example, top.img_ref = tkimg) or to a module-level dict prevents garbage collection. Many environments require full or correct relative paths; filenames are case-sensitive on Linux. Most Tkinter built-in PhotoImage handlers accept GIF/PPM; Pillow (PIL) is recommended for PNG/JPG or for resizing. Loading all images at startup wastes memory; lazy-loading the file on button press is better for many signs.

A compact, practical pattern (Python 3 + Pillow) — loop-create the buttons, lazy-load on click, keep one reference per open window:

import os
from tkinter import Tk, Button, Toplevel, Label, messagebox
from PIL import Image, ImageTk

SIGNS = [("Jan 20-Feb 18","Pisces.gif"), ("Feb 19-Mar 20","Taurus.gif")]  # add rest

def open_sign(path):
    if not os.path.exists(path):
        messagebox.showerror("Missing file", path)
        return
    top = Toplevel()
    img = Image.open(path)
    img.thumbnail((400, 400), Image.LANCZOS)
    tkimg = ImageTk.PhotoImage(img)
    top.img_ref = tkimg
    Label(top, image=tkimg).pack()

root = Tk()
for label, fname in SIGNS:
    Button(root, text=label, command=lambda f=fname: open_sign(f)).pack(padx=8, pady=6)
root.mainloop()

Quick checklist: confirm the interpreter (Python 2 uses Tkinter, Python 3 uses tkinter), verify file paths and extensions, prefer lazy loading and thumbnailing to limit memory, avoid an enormous fixed geometry (500x3000) — consider a normal window size with scrolling if needed, and include image files when distributing the app.

Recommended Answers

All 3 Replies

Here is the code format.

from Tkinter import *     #Call all modules for TKinter in order to make GUI.

app = Tk() #Define a variable for the TKinter interface.
app.title("Horoscope") #Apply GUI title
app.geometry("500x3000") #Size of window.

button1=Button(app, text="January 20-Febuary 18",width=20)
button1.pack(padx=15,pady=15)

button2=Button(app, text="February 19-March 20",width=20)
button2.pack(padx=15,pady=15)

button3=Button(app, text="March 21-April 19",width=20)
button3.pack(padx=15,pady=15)

button4=Button(app, text="April 20-May 20",width=20)
button4.pack(padx=15,pady=15)

button5=Button(app, text="May 21-June 20",width=20)
button5.pack(padx=15,pady=15)

button6=Button(app, text="June 21-July 22",width=20)
button6.pack(padx=15,pady=15)

button7=Button(app, text="July 23-August 22",width=20)
button7.pack(padx=15,pady=15)

button8=Button(app, text="August 23-September 22",width=20)
button8.pack(padx=15,pady=15)

button9=Button(app, text="September 23-October 22",width=20)
button9.pack(padx=15,pady=15)

button10=Button(app, text="October 23-November 21",width=20)
button10.pack(padx=15,pady=15)

button11=Button(app, text="November 22-December 21",width=20)
button11.pack(padx=15,pady=15)

button12=Button(app, text="December 22-January 19",width=20)
button12.pack(padx=15,pady=15)

app.mainloop() #Final input for making GUI

Here is a hint ...

''' toplevel101.py

'''

from functools import partial
from Tkinter import *     

def show_pic(img):
    # create child window
    top = Toplevel()
    Label(top, image=img).pack() 


# create the root window
app = Tk()
#Apply GUI title
app.title("Horoscope")
#Size of window.
app.geometry("500x3000") 

# pick images you have in the working directory or give full path
image1 = PhotoImage(file='1.gif')
cmd1 = partial(show_pic, image1)
button1=Button(app, text="January 20-Febuary 18",width=20,command=cmd1)
button1.pack(padx=15,pady=15)

image2 = PhotoImage(file='2.gif')
cmd2 = partial(show_pic, image2)
button2=Button(app, text="February 19-March 20",width=20,command=cmd2)
button2.pack(padx=15,pady=15)


# modify rest of buttons in similar fashion

button3=Button(app, text="March 21-April 19",width=20)
button3.pack(padx=15,pady=15)

button4=Button(app, text="April 20-May 20",width=20)
button4.pack(padx=15,pady=15)

button5=Button(app, text="May 21-June 20",width=20)
button5.pack(padx=15,pady=15)

button6=Button(app, text="June 21-July 22",width=20)
button6.pack(padx=15,pady=15)

button7=Button(app, text="July 23-August 22",width=20)
button7.pack(padx=15,pady=15)

button8=Button(app, text="August 23-September 22",width=20)
button8.pack(padx=15,pady=15)

button9=Button(app, text="September 23-October 22",width=20)
button9.pack(padx=15,pady=15)

button10=Button(app, text="October 23-November 21",width=20)
button10.pack(padx=15,pady=15)

button11=Button(app, text="November 22-December 21",width=20)
button11.pack(padx=15,pady=15)

button12=Button(app, text="December 22-January 19",width=20)
button12.pack(padx=15,pady=15)

# start the GUI event loop
app.mainloop() 

As an exercise you could create all those buttons in a loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.