Python Forum
why my list changes to a string as I move to another window in tkinter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why my list changes to a string as I move to another window in tkinter?
#1
Hello, I am learning how to work around moving in pages in tkinter. I asked a question a few days ago about that here: https://python-forum.io/thread-36382.html
I wrote another app using that basis learned there, and this app reads a text file and removes the redundant lines, and returns a list of numbers. The code below returns a list of numbers in PageOne, But as I want to use that list in PageTwo, it changes to a string. I know I have initially introduced variables as strings, but it does not work under other types as well. How I can keep that as a list?
To run this code you can put a few numbers with space separation in a text file and open it using the app.

import tkinter as tk
from tkinter import filedialog


class Data:
    def __init__(self):
        self.Tot_grids = tk.IntVar()
        self.NO = tk.StringVar()
        self.NEW = tk.StringVar()
        self.NEW2 = tk.StringVar()


class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.geometry("500x200")

        container = tk.Frame(self)
        container.pack()

        self.data = Data()

        self.frames = {}
        for F in (PageOne, PageTwo):
            frame = F(container, self.data)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.frames[PageOne].button2.config(command=self.go_to_page_two)
        self.show_frame(PageOne)

    def go_to_page_two(self):
        self.show_frame(PageTwo)

    def show_frame(self, c):
        frame = self.frames[c]
        frame.tkraise()


class PageOne(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data

        def opentext():
            my_file = filedialog.askopenfilenames(initialdir="/pycharm", title="Select your file")
            for T in my_file:  # this line should be here when opening multiple files
                import re
                with open(T, 'r') as infile1:
                    lines = infile1.read()

                    nums = []
                    for n in re.findall(r'(\d+\.\d+|\d+\*\d+)', lines):
                        split_by_ast = n.split("*")
                        if len(split_by_ast) == 1:
                            nums += [float(split_by_ast[0])]
                        else:
                            nums += [float(split_by_ast[1])] * int(split_by_ast[0])
                nums = nums[1:len(nums)]
                data.NO.set(nums)
                label3.config(text=nums)
                label4.config(text=type(nums))

        self.button1 = tk.Button(self, text="Open file(s)", command=opentext)
        self.button1.grid(row=0, column=0, columnspan=2, pady=20)

        label1 = tk.Label(self, text="imported numbers:")
        label1.grid(row=1, column=0, pady=10)

        label2 = tk.Label(self, text="type of imported numbers:")
        label2.grid(row=2, column=0, pady=5)

        label3 = tk.Label(self)
        label3.grid(row=1, column=1)

        label4 = tk.Label(self)
        label4.grid(row=2, column=1, pady=5)

        self.button2 = tk.Button(self, text="Continue")
        self.button2.grid(row=3, column=0, columnspan=2, pady=10)

class PageTwo(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data

        self.label5 = tk.Label(self, textvariable=self.data.NEW)
        self.label5.pack()

        self.label5 = tk.Label(self, textvariable=self.data.NEW2)
        self.label5.pack()

        self.data.NO.trace_add('write', self.on_grids_updated)

    def on_grids_updated(self, *args):
        self.data.NEW.set(self.data.NO.get())
        self.data.NEW2.set(type(self.data.NO.get()))

app = SampleApp()
app.mainloop()
I also wondered why the type of list in PageOne is shown with some random numbers? thanks
Reply
#2
Data.NO is a StringVar. Data.NO.set(thing) converts thing to a str because that is the kind of value you can save in StringVar.

When you set the text for a Label, you need to supply a str. "label.config(text=thing)" does not convert thing to a str like print(thing) or StringVar.set(thing) does.
Reply
#3
(Feb-17-2022, 04:53 AM)deanhystad Wrote: Data.NO is a StringVar. Data.NO.set(thing) converts thing to a str because that is the kind of value you can save in StringVar.

When you set the text for a Label, you need to supply a str. "label.config(text=thing)" does not convert thing to a str like print(thing) or StringVar.set(thing) does.

Sorry, I didn't quite understand. When I change Data.NO to another type such as IntVar or DoubleVar, it still doesn't work. Could you please show me within the code?
Reply
#4
None of those types can hold a list. You will have to come up with a different way that doesn't use tkinter variables.

I wonder why you are working with lists when none of the tkinter widgets you are using are capable of displaying or editing list information.
Reply
#5
(Feb-17-2022, 06:10 AM)deanhystad Wrote: None of those types can hold a list. You will have to come up with a different way that doesn't use tkinter variables.

I wonder why you are working with lists when none of the tkinter widgets you are using are capable of displaying or editing list information.

I dont know really. I think because I am very new to tkinter. :))
Do you have any suggestions for me?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 540 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 870 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,906 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,928 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,943 Apr-16-2022, 04:04 PM
Last Post: DBox
  [Tkinter] Tkinter Window Has no Title Bar gw1500se 4 2,873 Nov-07-2021, 05:14 PM
Last Post: gw1500se
  "tkinter.TclError: NULL main window" Rama02 1 5,879 Feb-04-2021, 06:45 PM
Last Post: deanhystad
  function in new window (tkinter) Dale22 7 5,196 Nov-24-2020, 11:28 PM
Last Post: Dale22
  Scrollable big image in a window (TKinter) Prospekteur 3 4,516 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  Run more than one window together with tkinter SmukasPlays 1 1,879 Aug-24-2020, 05:00 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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