-1

I am attemting to pass the variable dataLength into a label in the third of my Tkinter windows. the goal is to end up having a label that says "the length of your data set is:" then have the value of the dataLength variable after "is:". here is all my current code:

# imports
from statistics import mode
from statistics import median
from tkinter import *

# The Code For The GUI
data = ''
# function that starts the third window
def start_w_3():
    root3 = Tk()
    root3.geometry("400x300")
    app3 = Window3(root3)
    root3.mainloop()

# function that starts the second window
def start_w_2():
   root2 = Tk()
   root2.geometry("400x300")
   app2 = Window2(root2)
   root2.mainloop()

# making the third window
class Window3(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window3()

    def init_window3(self):
        self.master.title("data analyzer to go")
        self.pack(fill=BOTH, expand=1)
        lengthL = Label(self, text="The length of your data set is: ")
        lengthL.pack()

# making the second window
class Window2(Frame):

   def __init__(self, master=None):
       Frame.__init__(self, master)
       self.master = master
       self.init_window2()

   def init_window2(self):
       self.master.title("Data Analyzer To Go")
       self.pack(fill=BOTH, expand=1)
       viewButton = Button(self, text="View Results", command=start_w_3)
       viewButton.place(x=163, y=125)

# making the first window
class Window1(Frame):

   def __init__(self, master=None):
       Frame.__init__(self, master)
       self.master = master
       self.init_window1()

   def init_window1(self):
       self.master.title("Data Analyzer To Go")
       self.pack(fill=BOTH, expand=1)
       analyzeButton = Button(self, text="Analyze Data", command=lambda:[self.getData(), start_w_2()])
       dataPrompt = Label(self, text="Please enter your data")
       global data
       self.var=StringVar(self)
       e = Entry(self, textvariable=self.var)
       analyzeButton.place(x=150, y=150)
       e.place(x=125, y=125)
       dataPrompt.place(x=125, y=100)
   def getData(self):
       global data
       data=self.var.get()

root1 = Tk()
root1.geometry("400x300")
app1 = Window1(root1)
root1.mainloop()

# variables
data = [int(n) for n in data.split(' ')]
data.sort()
dataLength = len(data)
dataInt = list(map(int, data))
dataTotal = sum(dataInt)
dataMean = dataTotal / dataLength
highNumber = max(data)
lowNumber = min(data)
dataRange = highNumber - lowNumber

# telling length of data
#print("there is this many numbers in your data set: ")
print(dataLength, "\n")

# data sorted
print("your data in ascending order is: ")
print(data, "\n")

# total of data
print("the total of your data is: ")
print(dataTotal, "\n")

# mean of data
print("the mean of your data is: ")
print(dataMean, "\n")

# code to find and display data mode
try:
   dataMode = mode(data)
   print("the mode of your data is: ")
   print(dataMode, "\n")
except:
   StatisticsError:\
       print("no mode found. \n")

# code to find and display median of data.
try:
   dataMedian = median(data)
   print("the median of your data is: ")
   print(dataMedian, "\n")
except:
   StatisticsError:\
       print("no median found. \n")

# display the range of the data
print("the range of your data set is: ")
print(dataRange)

Just a review of the question, I am trying to have the dataLength variable passed into a label in the third window so that the third window contains: "The length of you data set is: " then the value of dataLength. any help is appreciated, Thanks!

jacky d
  • 5
  • 3
  • 2
    [You shouldn't really have multiple instances of Tk](https://stackoverflow.com/q/48045401/7032856). – Nae Mar 16 '18 at 12:51
  • 2
    At least be able to print dataLength as is first. This is a terrible way to ask a question as the post lacks a proper [mcve]. – Nae Mar 16 '18 at 13:26
  • @Mike-SMT: the statement _"If you run multiple Tk() instances you will not be able to pass data between them"_ is a bit too broad. You can definitely pass some data between them, it's just that they can't share references to the same tkinter variables and widgets. – Bryan Oakley Mar 16 '18 at 13:53
  • @BryanOakley Thanks for clarifying that. I have removed my comment as I would not want to put out info that is not clear enough or not completely accurate. – Mike - SMT Mar 16 '18 at 14:03
  • Please notice the [edit] button on the bottom left corner for revising your question. – Nae Mar 16 '18 at 16:11
  • 1
    It is alright. the answer given by mike has done exactly what i wanted. and i have learned a little bit more about how to properly ask questions. thank you for your time, and have a great day! – jacky d Mar 16 '18 at 16:22

1 Answers1

1

I have taken out all the code that is irrelevant to the question. Please note that any code that comes after the mainloop() will not run until the Tk() application is terminated. That said there are a few changes you may want to make. You do not need to call on a global function to start a new class to open a new window. You could actually do this all within one class but to keep things close to what you are doing I will use 3 class's to produce the results you are looking for.

First thing I did was move your variables into the first class Window1 as we are working with classes and shouldn't need to use global at all here. You also want to avoid using global whenever you can. Because we have moved the variables into the class and made them a class attribute we can reference those attributes by calling the name of the Window1 class (in this case app1) and then specify the variable we want to access.

Next I removed the 2 functions that created window 2 and window 3 and just moved the call directly to the class's Window2 and Window3.

Next we want to remove the extra instances of Tk() as you should only ever have 1 instance of Tk() in a Tkinter GUI. Instead we can use Toplevel() to create the new windows and because we are passing the master variable on from Window1 the different classes can communicate with each other without issue.

Take a look at the below reworked version of your code and let me know if you have any questions. The below code could likely be reduced further or refined a bit but should serve as a good example of what you can do in this situation.

Please note when asking questions we do not need all your code. Just the simplest form of your code that can produce the same problem you are having.

from tkinter import *

class Window3(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.root3 = Toplevel(master)
        self.root3.geometry("400x300")
        self.root3.title("Data Analyzer To Go 3")
        lengthL = Label(self.root3, text="The length of your data set is: {}".format(app1.dataLength))
        lengthL.pack()

class Window2(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.root2 = Toplevel(master)
        self.root2.geometry("400x300")
        self.root2.title("Data Analyzer To Go 2")
        viewButton = Button(self.root2, text="View Results", command=lambda x=master:Window3(x))
        viewButton.place(x=163, y=125)

class Window1(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.master = master
        self.master.title("Data Analyzer To Go")
        self.pack(fill=BOTH, expand=1)
        analyzeButton = Button(self, text="Analyze Data", command=lambda x=master:[self.getData(), Window2(x)])
        dataPrompt = Label(self, text="Please enter your data")
        self.var=StringVar(self)
        e = Entry(self, textvariable=self.var)
        analyzeButton.place(x=150, y=150)
        e.place(x=125, y=125)
        dataPrompt.place(x=125, y=100)

    def getData(self):
        self.data=self.var.get()
        self.data = [int(n) for n in self.data.split(' ')]
        self.data.sort()
        self.dataLength = len(self.data)

root1 = Tk()
root1.geometry("400x300")
app1 = Window1(root1)
root1.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • 1
    I apologize for my badly written question. I am still in the process of learning coding and how the community operates. as of now I am only 13 so I hope that I can improve in the future. thank you for your help and understanding, and I hope you have a great Friday! – jacky d Mar 16 '18 at 15:21
  • 1
    @jackyd for 13 you are doing well. Keep it up if you are doing this at 13 by the time you are old enough to work you will be a great programmer. – Mike - SMT Mar 16 '18 at 15:25
  • 1
    Thank you very much @Mike - SMT. It is very nice to hear that as i look forward to pursuing programming as a career. – jacky d Mar 16 '18 at 16:24