Python Forum
Unable fetch fucntion data in class in tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable fetch fucntion data in class in tkinter
#1
I am trying to fetch the data from text variable and image data in a function name loopCap and display it in __init__ function where I have created a canvas but due to mentioned below error I am not able to move further

Update: I was able to update the text but Image is still the major issues

For Text Error:

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

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

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()
Reply
#2
text variables need to be of type StringVar()
fetch value with varname.get
Reply
#3
Hi, I was able to update the text data using canvas.itemconfig() but still facing the issue of calling image data from loopCap function in __init__ function as I have edited my main question and updated the image error which I am still facing there.
Please help me in resolving this issue. Will be very helpful
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Smile Unable to display Devanagari ligatures correctly in python tkinter Anamika 4 1,407 Nov-26-2024, 08:08 PM
Last Post: woooee
Lightbulb Using Tkinter With Concurrent.Futures / ThreadPoolExecutor Class AaronCatolico1 1 2,619 Dec-14-2022, 08:01 PM
Last Post: deanhystad
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 6,839 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 3,635 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  tkinter moving an class object with keybinds gr3yali3n 5 5,739 Feb-10-2021, 09:13 PM
Last Post: deanhystad
  [Tkinter] Troubles with accessing attr from other class zarize 3 3,466 Aug-20-2020, 06:05 PM
Last Post: deanhystad
  [Tkinter] Use function from other class (Tkinter) zarize 8 6,990 Aug-17-2020, 09:47 AM
Last Post: zarize
  Tkinter:Unable to bind and unbind function with a button shallanq 2 6,094 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  Fetch records from DB and display on GUI hnkrish 1 2,346 Dec-05-2019, 02:38 PM
Last Post: Denni
  Unable to put background image on Tkinter Frame jenkins43 2 9,774 Nov-27-2019, 11:38 AM
Last Post: jenkins43

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020