简体   繁体   中英

How can I get a Tkinter entry widget to work the same as a normal python input

I am trying to get the Tkinter entry, 'data' to work with all the variables I have set up. right now I am using a normal python input but I am trying to convert my project to a GUI project. so when I take out the input for data and attempt to use the Entry for data I get errors saying that there is no split or sort attribute and such. i would like for the GUI version of my program to work the same as the terminal version. is this possible? this is all the code that should be needed to answer the question:

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

# The Code For The GUI
# class that makes the window
class Window(Frame):

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

    def init_window(self):
        self.master.title("Data Analyzer To Go")
        self.pack(fill=BOTH, expand=1)
        analyzeButton = Button(self, text="Analyze Data")
        dataPrompt = Label(self, text="Please enter your data")
        global data
        data = Entry(self)
        analyzeButton.place(x=150, y=150)
        data.place(x=125, y=125)
        dataPrompt.place(x=125, y=100)

root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()

# variables
data = input("please enter your data set: ")
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

Again my goal is to have the Entry do the same thing as the Input would. Any help is greatly appreciated! Thanks!

I modified your code to get string values for your data variable. Now you type in the numbers in the entry field separated by space and when you click on the Analyze Data button it saves the entry into your global data variable. The command method of the button is taking care of putting the entry data into that variable. This way there is no need to use the input line anymore. I defined data='' in the beginning to have a global variable. Just as a side note I remark it is not the best practice to rely on global variables.

data = ''

class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()

    def init_window(self):
        self.master.title("Data Analyzer To Go")
        self.pack(fill=BOTH, expand=1)
        analyzeButton = Button(self, text="Analyze Data", command=self.getData)
        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()

root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()

# variables
#data = input("please enter your data set: ")
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM