无法在错误 tkinter 中使用几何管理器包 [英] cannot use geometry manager pack inside error tkinter

查看:33
本文介绍了无法在错误 tkinter 中使用几何管理器包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的 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()

推荐答案

在 tkinter 中,.grid().pack() 不能在同一个窗口中使用.要解决这个问题,你必须选择只使用 .grid.pack().

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().

接近尾声时,您编写了 label.pack().相反,使用 label.grid(row=5) 或任何你想放置标签的地方.

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

如果您仍然想同​​时使用两者,您可以将所有使用 .grid() 的小部件放入一个 Frame 小部件中.然后,您可以打包两个框架.

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.

这篇关于无法在错误 tkinter 中使用几何管理器包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆