skywalker
skywalker

Reputation: 23

cannot use geometry manager pack inside error tkinter

ı am new tkinter. I want to add a image to my programme. But ı am taking a error:cannot use geometry manager pack inside . which already has slaves managed by grid. If ı run this code individual it is working. I can obtain image and programme is working. But if ı run together image and function it is not working. How can ı fix it?

from tkinter import *
from PIL import ImageTk,Image
import tkinter as tk
from functools import partial
root=tk.Tk()
root.geometry("900x900+100+200")
root.title("Converter")
root.configure(background="grey")
root.resizable(width=False,height=False)
lenghtVal="Lenght"
def store_lenght(sel_lenght):
  global lenghtVal
  lenghtVal=sel_lenght
def call_result(rL,inputn):
 lenght=inputn.get()
 if lenghtVal=="Angström-Milimetre":
    mm=float((float(lenght)*10**-7))
    rL.config(text="% f milimetre" % mm)

 if lenghtVal=="Yard-Metre":
    m=float((float(lenght)*0.9144))
    rL.config(text="% f metre" % m)


 if lenghtVal=="Inch-Metre":      
    m=float((float(lenght)*0.0254))
    rL.config(text="% f metre" % m)

 if lenghtVal=="Mil-Metre":
    km=float((float(lenght)*1.6903))
    rL.config(text="% f kilometre" % km)
 return
numberInput=tk.StringVar()
var=tk.StringVar()
input_label=tk.Label(root,text="Enter",background="white",foreground="black")
input_entry=tk.Entry(root,textvariable=numberInput)
input_label.grid(row=0)
input_entry.grid(row=0,column=1)
rLabel=tk.Label(root,text="0.0",background="white")
rLabel.grid(row=4,columnspan=2)
call_result=partial(call_result,rLabel,numberInput)

result_button=tk.Button(root,text="convert",command=call_result,background="white",foreground="black")
result_button.grid(row=2,columnspan=2)
dropdownList=["-Uzunluk Ölçüleri-","Angström-Milimetre","Yard-Metre","Inch-Kilometre","Mil-Metre"]
dropdown=tk.OptionMenu(root,var,*dropdownList,command=store_lenght)
dropdown.grid(row=0,column=2)
var.set(dropdownList[0])

root=Tk()

root.geometry("1255x944")
image=Image.open("C:\\Users\\Asus\\Desktop\\6.png")
photo=ImageTk.PhotoImage(image)
label=Label(image=photo)
label.pack()
root.mainloop()

Upvotes: 1

Views: 1740

Answers (1)

Justin Cheng
Justin Cheng

Reputation: 253

In tkinter, .grid() and .pack() cannot be used in the same window. To solve the problem, you have to choose to either use only .grid or .pack().

Near the end, you wrote label.pack(). Instead, use label.grid(row=5) or wherever you want to put your label.

If you still want to use both, you can put all of your widgets that use .grid() into a Frame widget. Then, you can pack both frames.

Upvotes: 1

Related Questions