Hi i'm a beginner in python and I really got int trouble with some methods, I wanna give some number from Entry of tkinter class and show them with a chart, but the thing is that I cant get int number: so the chart wont work [here is the picture of my code , I get some bumber from entry but i cant make them integer number] 1: https://i.stack.imgur.com/2Vuvn.jpg 2: https://i.stack.imgur.com/Pa23V.jpg
CodePudding user response:
Welcome. I'm posting a complete, I think, answer to this question but there are a couple of etiquette things you should know:
- Please don't post screenshots of your code. Copy and paste into the editor.
- Please post just enough code to show your problem, but which is complete enough that we can just copy it into our own editors / IDEs and run without a lot of modification.
- The previous commenters are correct that this question has probably been answered a hundred times, so please try to search through previous answers before posting your question.
Having said that, I have not answered this question before, so here's my rendition. I know you're a beginner so I've tried to keep it as simple as possible, but you're also tackling TKinter so I've not made it overly simplistic.
import tkinter as tk
def main():
global entryVar, lableVar
#create a tkinter window:
rootWin = tk.Tk() #creates a root window
rootWin.title('Entry Test') #shows text on the title bar
rootWin.geometry('500x200') #sets the displayable size of the window
#we'll need these variables and they MUST be tk.StringVar()
entryVar = tk.StringVar() #variable to hold the entry value
lableVar = tk.StringVar() #variable to hold the lable value
#create an entry widget:
entry = tk.Entry(
rootWin,
width = 5,
textvariable = entryVar
)
entry.pack(expand=1)
entry.bind('<Return>', getEntryValue) #bind enter key to widget
entry.bind('<KP_Enter>', getEntryValue, add=' ') #bind the other enter key to widget
#create a lable widget
lable = tk.Label(
rootWin,
textvariable = lableVar
)
lable.pack(expand=1)
lableVar.set("This is where the lable is.")
entry.focus_set() #set focus on the entry widget for convenience
rootWin.mainloop()
def getEntryValue(event):
global entryVar, lableVar
x = entryVar.get() #get the value from Entry
x = int(x) #change it to an int
lableVar.set(x) #set the lable variable
entryVar.set("") #clear the entry variable
if __name__ == "__main__":
main()
So, what's going on here is that we make a window in the usual way. I've created both an Entry()
widget to get some input, and a Label()
widget to show whatever has been input. I've broken the Entry()
and Label()
declarations up over multiple lines just to make them easier to read.
You can attach variables to many TKinter widgets to that you can .get()
and .set()
their values more easily, but they almost always need to be TKinter variable types such as StringVar()
or IntVar()
. I've created two such variables, one for the Entry()
widget and another for the Label()
widget.
I've also added "bindings" to the Entry()
widget to both show how that works and to make data entry a bit more convenient. I don't know if you have a separate number pad on your computer keyboard so I've bound both the main <enter>
key as well as the number pad's <enter>
key. When you hit either one of those keys, the Entry()
widget will call the getEntryValue()
function which does the work of getting the value and displaying it on the window.
For convenience, entry.focus_set()
immediately puts the focus on the Entry()
widget, then the TKinter window enters the .mainloop()
to do its stuff.
The getEntryValue()
function is called by the events which we set on the Entry()
widget. I broke it down into more lines than necessary to illustrate what needs to happen. First we retrieve the value of the Entry()
widget through its variable, entryVar
. You do that using entryVar
's .get()
method: x = entryVar.get()
. That returns a string value which you will have to convert to an integer using the normal int()
function available in Python. For this purposes of this demonstration I've chosen to display that value to a Label()
widget which I've placed in the window, so I use the Label()
widget's variable lableVar
: lableVar.set(x)
. You don't have to convert the integer back into a string before doing this.
I then clear out the entryVar
variable so that there isn't anything left in the Entry()
widget to get in the way of our next entry.
I've used entryVar
and lableVar
as globals just to simplify the example.
And that's how you do it.
CodePudding user response:
I guess the problem is here:
a=str(e3.get())
Try something like this:
a=int(e3.get())
Since what you want is an integer