【问题标题】:Unable fetch text variable data in class in tkinter无法在 tkinter 的类中获取文本变量数据
【发布时间】:2019-11-30 13:21:37
【问题描述】:

我正在尝试从函数名称loopCap 中的文本变量和图像中获取数据,并将其显示在我创建画布的__init__ 函数中,但由于下面提到的错误,我无法进一步移动

对于文字错误:

   *(args + self._options(cnf, kw))))
_tkinter.TclError: unknown option "-textvariable"

图片错误:

self.put_photo = canvas.create_image(70, 250, image=self.photo, anchor='nw')
AttributeError: 'StartPage' object has no attribute 'photo'

实际代码示例:

import tkinter as tk
from tkinter import *
from tkinter.filedialog import asksaveasfile
from tkinter import messagebox
from PIL import ImageTk, Image

im = "BG.jpg"
class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.ard_stat = read_json(JSON_PATH)
        self.switch_frame(StartPage)

    def switch_frame(self, frame_class):
        """Destroys current frame and replaces it with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()

class StartPage(tk.Frame):

    def loopCap(self):
        with open(JSON_PATH) as json_file1:
            self.data = json.load(json_file1)
            print(self.data)
        if self.data['status'] == 'ACTIVE':  # and (self.data['RH_img']!= 'null' or self.data['LH_img']!= 'null')
            a = self.text.set(self.data['status'])
         if self.data['RH_img'] == 'NA':
         self.img = Image.open(Press)
         self.img.load()
       self.photo = ImageTk.PhotoImage(self.img)
       self.label['image'] = self.photo
       self.master.after(500, self.loopCap)

 def __init__(self, master):
        super().__init__(master)
        self.master.config(bg='black')
        Frame2 = tk.Frame(self.master)
        Frame2.pack(fill='both',expand = True)
        self.photo_back = ImageTk.PhotoImage(im)
        lab_1 = tk.Label(Frame2, image=self.photo_back)
        lab_1.image = self.photo_back
        lab_1.place(x=0, y=0, relwidth=1, relheight=1)
        canvas = Canvas(Frame2, width=1000, height=700)
        canvas.place(x=0, y=0, relwidth=1, relheight=1)
        canvas.create_image(0, 0, image=self.photo_back, anchor='nw')
        self.text = tk.StringVar()
        canvas.create_text(230, 170, fill="white", font="Times 20 bold", 
        textvariable=self.text, anchor="w")
        self.label = tk.Label(canvas)
        self.put_photo = canvas.create_image(70, 250, image=self.photo, anchor='nw')
        canvas.itemconfig(self.put_photo, image=self.photo)
        self.master.after(500, self.loopCap)


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

任何建议都会有很大帮助。

【问题讨论】:

  • 嗨,你能分享一些例子或者我应该如何在代码中更新它,因为我是新手
  • “一些例子”:阅读给定的链接和itemconfig
  • 谢谢,它适用于文本,但当我尝试更新图像时同样的事情然后我面临错误self.put_photo = canvas.create_image(70, 250, image=self.photo, anchor='nw') AttributeError: 'StartPage' object has no attribute 'photo' 我可以用这个更新图像吗?
  • “我可以用这个更新图片吗?”:是的,你的说法没问题,但你有photo = ImageTk.PhotoImage(im)。应该阅读self.photo = ImageTk....

标签: python python-3.x tkinter tkinter-canvas


【解决方案1】:

尝试将“textvariable=”替换为“text="

canvas.create_text(230, 170, fill="white", font="Times 20 bold", text=self.text, anchor="w")

【讨论】:

    猜你喜欢
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多