This is my code:

from tkinter import *

def ftm(feet):
    return feet * 0.305

def mtf(meters):
    return meters / 0.305

def convert(amount, func, result):
    value = func(float(amount.get()))
    result.set(round((value), 3))

def choose():
    choice = lol.get()
    if choice == "ft":
        feet = StringVar()
        meters = StringVar()
        rest = Toplevel()
        f3 = Frame(rest)
        f3.grid(column=0, row=0, sticky=(N,W,E,S))
        f3.columnconfigure(0, weight=1)
        f3.rowconfigure(0, weight=1)
        feet_entry = Entry(f3, width=7, textvariable=feet)
        feet_entry.grid(column=2, row=1, sticky=(W, E))
        Label(f3, text="feet").grid(column=3, row=1, sticky=W)
        Label(f3, text="is equal to").grid(column=1, row=2, sticky=E)
        Label(f3, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
        Label(f3, text="meters").grid(column=3, row=2, sticky=W)
               command=lambda a=feet_entry, f=ftm, r=meters: convert(a, f, r)
              ).grid(column=2, row=3, sticky=W)
    elif choice == "m":
        meters = StringVar()
        feet = StringVar()
        pepp = Toplevel()
        f4 = Frame(pepp)
        f4.grid(column=0, row=0, sticky=(N,W,E,S))
        f4.columnconfigure(0, weight=1)
        f4.rowconfigure(0, weight=1)
        meter_entry = Entry(f4, width=7, textvariable=meters)
        meter_entry.grid(column=2, row=1, sticky=(W, E))
        Label(f4, text="meters").grid(column=3, row=1, sticky=W)
        Label(f4, text="is equal to").grid(column=1, row=2, sticky=E)
        Label(f4, textvariable=feet).grid(column=2, row=2, sticky=(W, E))
        Label(f4, text="feet").grid(column=3, row=2, sticky=W)
               command=lambda a=meter_entry, f=mtf, r=feet: convert(a, f, r)
              ).grid(column=2, row=3, sticky=W)
        rino = Toplevel()
        f5 = Frame(rino)
        f5.grid(column=0, row=0, sticky=(N,W,E,S))
        Label(f5, text="ERROR!").grid(column=1, row=1)

rip = Tk()
f2 = Frame(rip)
f2.grid(column=0, row=0, sticky=(N,W,E,S))
f2.columnconfigure(0, weight=1)
f2.rowconfigure(0, weight=1)
c1=Label(f2, text="feet to meter or meter to feet?")
c1.grid(row=0, column=0)
c2=Label(f2, text='type "ft" for feet to meter, type "m" for meter to feet')
c2.grid(row=1, column=0)
lol = StringVar()
choice=Entry(f2, width=7, textvariable=lol)
choice.grid(row=2, column=0)
b1=Button(f2, text="Confirm", command=choose)
b1.grid(row=3, column=0)


I want to add background image for all these windows. How do I do it?

Recommended Answers

All 4 Replies

Here is an example ...

# use a Tkinter label as a panel/frame with a background image
# (note that Tkinter reads only GIF and PGM/PPM images)
# put a button on the background image

    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()
root.title('background image')

# pick a .gif image file you have in the working directory
# or give full path to the image file
image = tk.PhotoImage(file="roses.gif")
# get the width and height of the image
w = image.width()
h = image.height()
# position coordinates of root 'upper left corner'
x = 200
y = 50
# size the root to fit the image
root.geometry("%dx%d+%d+%d" % (w, h, x, y))

# tk.Frame has no image argument
# so use a label as a panel/frame
panel = tk.Label(root, image=image)
panel.pack(side='top', fill='both', expand='yes')

# put a button widget on the panel
button = tk.Button(panel, text='button widget')
button.pack(side='top', pady=5)

# save the panel's image from 'garbage collection'
panel.image = image

# start the event loop

Does that mean if I use tk.Tk(),then all my label, entry and button will need to change to tk.Label, tk.Entry, tk.Button?

Yes because Vegaseat uses

import tkinter as tk

while you are using

from tkinter import *

Usually, people prefer to avoid import * because there is no way to see which names are imported in the current namespace. With the first import, only the variable tk is added to the current namespace.

Avoiding import * is a good rule, although very good tkinter programs have been written with this import.

Thank both of you guys! Really appreciate the help!

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.