【问题标题】:tkinter send text from the label to entry boxtkinter 将文本从标签发送到输入框
【发布时间】:2020-09-22 02:20:26
【问题描述】:

如何将标签中的文本发送到输入框,或者直接将图像的路径发送到tkinter中的输入框?

from tkinter import *

from PIL import ImageTk,Image

from tkinter import filedialog

root = Tk()

def open():

    root.filename = filedialog.askopenfilename(initialdir="/", title="Select A File",filetypes=(("jpg files", "*.jpg"), ("all files", "*.*")))

    my_label = Label(root, text=root.filename).pack()

    my_btn = Button(root, text="Open files", command=open).pack()

#entry box

txt2 = Entry(root)

txt2.place(x=10, y=45)

root.mainloop()

【问题讨论】:

  • 我不明白,你是在调用按钮中的函数来再次调用相同的函数?

标签: python tkinter


【解决方案1】:

您的意思是在标签中获取文本并将文本插入到条目中吗?
如果是,您可以在条目中创建一个StringVar()
比如这样:

from tkinter import *         #For python 3                
from Tkinter import *         #For python 2             

root = Tk()         
txt = StringVar()            

#For inserting text
txt.set("This is a label and an entry")         
 
label = Label(root, textvariable=txt).pack()         

entry = Entry(root, textvariable=txt).pack()         

root.mainloop()

【讨论】:

  • 如果 OP 使用 python 3.x 或 2.x 两种方式都会出错,请使用 tryexcept
  • 这表明编程习惯很差。 labelentry 都将设置为 None,因此拥有这些变量毫无意义。
猜你喜欢
  • 1970-01-01
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
相关资源
最近更新 更多