How to use the last user input in Text widget in an If/Else statement after Enter is pressed?How to redirect print statements to Tkinter text widgetHow to get the input from the Tkinter Text Box Widget?How do I execute a callback when the Enter key is pressed after entering text into a Text widget?Python timer in math game TkinterProgram halts until the user enters text and presses ButtonPython: How to change text of a message widget to a user inputTkinter entry text justification after insertHow to wait for user to press enterHow to trace user input from first hit of the button in Text widget?How to update a widget after pressing the button in a different widget?
What's the Difference between Two Single-Quotes and One Double-Quote?
Why does the 6502 have the BIT instruction?
Why are these traces shaped in such way?
Can a Beholder use rays in melee range?
How were these pictures of spacecraft wind tunnel testing taken?
Where did Wilson state that the US would have to force access to markets with violence?
Do you play the upbeat when beginning to play a series of notes, and then after?
Why colon to denote that a value belongs to a type?
reinterpret_cast from rvalue to rvalue reference
I think I may have violated academic integrity last year - what should I do?
Is healing by fire possible?
I unknowingly submitted plagiarised work
How to prevent bad sectors?
Array Stutter Implementation
Integrating an absolute function using Mathematica
How can people dance around bonfires on Lag Lo'Omer - it's darchei emori?
How to capture more stars?
Should I disclose a colleague's illness (that I should not know about) when others badmouth him
Is CD audio quality good enough for the final delivery of music?
What are these (utility?) boxes at the side of the house?
Why does the 'metric Lagrangian' approach appear to fail in Newtonian mechanics?
What do different value notes on the same line mean?
Is floating in space similar to falling under gravity?
Mother abusing my finances
How to use the last user input in Text widget in an If/Else statement after Enter is pressed?
How to redirect print statements to Tkinter text widgetHow to get the input from the Tkinter Text Box Widget?How do I execute a callback when the Enter key is pressed after entering text into a Text widget?Python timer in math game TkinterProgram halts until the user enters text and presses ButtonPython: How to change text of a message widget to a user inputTkinter entry text justification after insertHow to wait for user to press enterHow to trace user input from first hit of the button in Text widget?How to update a widget after pressing the button in a different widget?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I've managed to get the user_input from the Entry field to be displayed on my Text widget when Enter key is pressed, but when I try to build nested if/statements using the last answer entered by the user, it doesn't seem to recognise it and it stops.
root = Tk()
...
Text and Entry widgets created
...
def Enter_pressed(event):
global input_get
input_get = input_field.get()
print(input_get)
messages.tag_config("right", justify="right")
messages.insert(INSERT, '%sn' % input_get, "right")
input_user.set('')
return "continue"
frame = Frame(root)
input_field.bind("<Return>", Enter_pressed)
frame.pack()
def question():
question1 = str(">>>Do you like apples?")
messages.insert(INSERT, '%sn' % question1)
if input_get == str("Yes") or input_get == str("yes"):
messages.insert(INSERT, ">>>Great")
else:
question2 = str(">>>How about peaches?")
messages.insert(INSERT, '%sn' % question2)
if input_get == str("Yes") or input_get == str("yes"):
messages.insert(INSERT, ">>>I like peaches too.")
else:
messages.insert(INSERT, ">>Have you tried mango?")
messages.after(5000, question)
root.mainloop()
python tkinter
add a comment |
I've managed to get the user_input from the Entry field to be displayed on my Text widget when Enter key is pressed, but when I try to build nested if/statements using the last answer entered by the user, it doesn't seem to recognise it and it stops.
root = Tk()
...
Text and Entry widgets created
...
def Enter_pressed(event):
global input_get
input_get = input_field.get()
print(input_get)
messages.tag_config("right", justify="right")
messages.insert(INSERT, '%sn' % input_get, "right")
input_user.set('')
return "continue"
frame = Frame(root)
input_field.bind("<Return>", Enter_pressed)
frame.pack()
def question():
question1 = str(">>>Do you like apples?")
messages.insert(INSERT, '%sn' % question1)
if input_get == str("Yes") or input_get == str("yes"):
messages.insert(INSERT, ">>>Great")
else:
question2 = str(">>>How about peaches?")
messages.insert(INSERT, '%sn' % question2)
if input_get == str("Yes") or input_get == str("yes"):
messages.insert(INSERT, ">>>I like peaches too.")
else:
messages.insert(INSERT, ">>Have you tried mango?")
messages.after(5000, question)
root.mainloop()
python tkinter
add a comment |
I've managed to get the user_input from the Entry field to be displayed on my Text widget when Enter key is pressed, but when I try to build nested if/statements using the last answer entered by the user, it doesn't seem to recognise it and it stops.
root = Tk()
...
Text and Entry widgets created
...
def Enter_pressed(event):
global input_get
input_get = input_field.get()
print(input_get)
messages.tag_config("right", justify="right")
messages.insert(INSERT, '%sn' % input_get, "right")
input_user.set('')
return "continue"
frame = Frame(root)
input_field.bind("<Return>", Enter_pressed)
frame.pack()
def question():
question1 = str(">>>Do you like apples?")
messages.insert(INSERT, '%sn' % question1)
if input_get == str("Yes") or input_get == str("yes"):
messages.insert(INSERT, ">>>Great")
else:
question2 = str(">>>How about peaches?")
messages.insert(INSERT, '%sn' % question2)
if input_get == str("Yes") or input_get == str("yes"):
messages.insert(INSERT, ">>>I like peaches too.")
else:
messages.insert(INSERT, ">>Have you tried mango?")
messages.after(5000, question)
root.mainloop()
python tkinter
I've managed to get the user_input from the Entry field to be displayed on my Text widget when Enter key is pressed, but when I try to build nested if/statements using the last answer entered by the user, it doesn't seem to recognise it and it stops.
root = Tk()
...
Text and Entry widgets created
...
def Enter_pressed(event):
global input_get
input_get = input_field.get()
print(input_get)
messages.tag_config("right", justify="right")
messages.insert(INSERT, '%sn' % input_get, "right")
input_user.set('')
return "continue"
frame = Frame(root)
input_field.bind("<Return>", Enter_pressed)
frame.pack()
def question():
question1 = str(">>>Do you like apples?")
messages.insert(INSERT, '%sn' % question1)
if input_get == str("Yes") or input_get == str("yes"):
messages.insert(INSERT, ">>>Great")
else:
question2 = str(">>>How about peaches?")
messages.insert(INSERT, '%sn' % question2)
if input_get == str("Yes") or input_get == str("yes"):
messages.insert(INSERT, ">>>I like peaches too.")
else:
messages.insert(INSERT, ">>Have you tried mango?")
messages.after(5000, question)
root.mainloop()
python tkinter
python tkinter
edited Mar 24 at 14:18
Flimzy
41.6k1369104
41.6k1369104
asked Mar 24 at 7:20
ViktorViktor
1
1
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I am trying to evangelize toward an oo approach... so look at the below script
import tkinter as tk
class App(tk.Frame):
def __init__(self,):
super().__init__()
self.master.title("Hello World")
self.option =tk.IntVar()
self.option.set(2)
self.questions = 0:"Do you like apples?",
1:"How about peaches?",
2:"I like peaches too.",
3:"Have you tried mango?",
4:"Great",
5:"End of the questions."
self.which = tk.IntVar()
self.which.set(0)
self.init_ui()
def init_ui(self):
self.pack(fill=tk.BOTH, expand=1,)
f = tk.Frame()
self.question = tk.Label(f, text = self.questions[0])
self.question.pack()
ops = ('Yes','No',)
for index, text in enumerate(ops):
tk.Radiobutton(f,
text=text,
variable=self.option,
command=self.callback,
value=index,).pack(anchor=tk.W)
w = tk.Frame()
tk.Button(w, text="Print", command=self.callback).pack()
tk.Button(w, text="Reset", command=self.on_reset).pack()
tk.Button(w, text="Close", command=self.on_close).pack()
f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)
def callback(self):
#print(self.option.get())
if self.which.get()==0:
if self.option.get() == 0:
self.which.set(4)
else:
self.which.set(1)
elif self.which.get()==1:
if self.option.get() == 0:
self.which.set(2)
else:
self.which.set(3)
else:
self.which.set(5)
a = self.questions[self.which.get()]
self.question.config(text=a)
self.option.set(2)
def on_reset(self):
self.which.set(0)
self.option.set(2)
a = self.questions[self.which.get()]
self.question.config(text=a)
def on_close(self):
self.master.destroy()
if __name__ == '__main__':
app = App()
app.mainloop()
This answer would be much better if you added an explanation. Otherwise we have to ready your code line-by-line and character-by-character to see how it's different from the code in the question.
– Bryan Oakley
Mar 24 at 13:13
add a comment |
Thanks for taking the time to reply. I tried your option and I wasn't able to make it work somehow.`
I tried to insert the nested if statements within the def Enter_presssed(event) code-block and it works up to a point. It recognises the first "if" and the last "else" but not the ifs and elses inbetween.
Shouldn't the program see and use each new input_get after Return is pressed as an answer to the last question displayed in the Text widget?
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55321555%2fhow-to-use-the-last-user-input-in-text-widget-in-an-if-else-statement-after-ente%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I am trying to evangelize toward an oo approach... so look at the below script
import tkinter as tk
class App(tk.Frame):
def __init__(self,):
super().__init__()
self.master.title("Hello World")
self.option =tk.IntVar()
self.option.set(2)
self.questions = 0:"Do you like apples?",
1:"How about peaches?",
2:"I like peaches too.",
3:"Have you tried mango?",
4:"Great",
5:"End of the questions."
self.which = tk.IntVar()
self.which.set(0)
self.init_ui()
def init_ui(self):
self.pack(fill=tk.BOTH, expand=1,)
f = tk.Frame()
self.question = tk.Label(f, text = self.questions[0])
self.question.pack()
ops = ('Yes','No',)
for index, text in enumerate(ops):
tk.Radiobutton(f,
text=text,
variable=self.option,
command=self.callback,
value=index,).pack(anchor=tk.W)
w = tk.Frame()
tk.Button(w, text="Print", command=self.callback).pack()
tk.Button(w, text="Reset", command=self.on_reset).pack()
tk.Button(w, text="Close", command=self.on_close).pack()
f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)
def callback(self):
#print(self.option.get())
if self.which.get()==0:
if self.option.get() == 0:
self.which.set(4)
else:
self.which.set(1)
elif self.which.get()==1:
if self.option.get() == 0:
self.which.set(2)
else:
self.which.set(3)
else:
self.which.set(5)
a = self.questions[self.which.get()]
self.question.config(text=a)
self.option.set(2)
def on_reset(self):
self.which.set(0)
self.option.set(2)
a = self.questions[self.which.get()]
self.question.config(text=a)
def on_close(self):
self.master.destroy()
if __name__ == '__main__':
app = App()
app.mainloop()
This answer would be much better if you added an explanation. Otherwise we have to ready your code line-by-line and character-by-character to see how it's different from the code in the question.
– Bryan Oakley
Mar 24 at 13:13
add a comment |
I am trying to evangelize toward an oo approach... so look at the below script
import tkinter as tk
class App(tk.Frame):
def __init__(self,):
super().__init__()
self.master.title("Hello World")
self.option =tk.IntVar()
self.option.set(2)
self.questions = 0:"Do you like apples?",
1:"How about peaches?",
2:"I like peaches too.",
3:"Have you tried mango?",
4:"Great",
5:"End of the questions."
self.which = tk.IntVar()
self.which.set(0)
self.init_ui()
def init_ui(self):
self.pack(fill=tk.BOTH, expand=1,)
f = tk.Frame()
self.question = tk.Label(f, text = self.questions[0])
self.question.pack()
ops = ('Yes','No',)
for index, text in enumerate(ops):
tk.Radiobutton(f,
text=text,
variable=self.option,
command=self.callback,
value=index,).pack(anchor=tk.W)
w = tk.Frame()
tk.Button(w, text="Print", command=self.callback).pack()
tk.Button(w, text="Reset", command=self.on_reset).pack()
tk.Button(w, text="Close", command=self.on_close).pack()
f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)
def callback(self):
#print(self.option.get())
if self.which.get()==0:
if self.option.get() == 0:
self.which.set(4)
else:
self.which.set(1)
elif self.which.get()==1:
if self.option.get() == 0:
self.which.set(2)
else:
self.which.set(3)
else:
self.which.set(5)
a = self.questions[self.which.get()]
self.question.config(text=a)
self.option.set(2)
def on_reset(self):
self.which.set(0)
self.option.set(2)
a = self.questions[self.which.get()]
self.question.config(text=a)
def on_close(self):
self.master.destroy()
if __name__ == '__main__':
app = App()
app.mainloop()
This answer would be much better if you added an explanation. Otherwise we have to ready your code line-by-line and character-by-character to see how it's different from the code in the question.
– Bryan Oakley
Mar 24 at 13:13
add a comment |
I am trying to evangelize toward an oo approach... so look at the below script
import tkinter as tk
class App(tk.Frame):
def __init__(self,):
super().__init__()
self.master.title("Hello World")
self.option =tk.IntVar()
self.option.set(2)
self.questions = 0:"Do you like apples?",
1:"How about peaches?",
2:"I like peaches too.",
3:"Have you tried mango?",
4:"Great",
5:"End of the questions."
self.which = tk.IntVar()
self.which.set(0)
self.init_ui()
def init_ui(self):
self.pack(fill=tk.BOTH, expand=1,)
f = tk.Frame()
self.question = tk.Label(f, text = self.questions[0])
self.question.pack()
ops = ('Yes','No',)
for index, text in enumerate(ops):
tk.Radiobutton(f,
text=text,
variable=self.option,
command=self.callback,
value=index,).pack(anchor=tk.W)
w = tk.Frame()
tk.Button(w, text="Print", command=self.callback).pack()
tk.Button(w, text="Reset", command=self.on_reset).pack()
tk.Button(w, text="Close", command=self.on_close).pack()
f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)
def callback(self):
#print(self.option.get())
if self.which.get()==0:
if self.option.get() == 0:
self.which.set(4)
else:
self.which.set(1)
elif self.which.get()==1:
if self.option.get() == 0:
self.which.set(2)
else:
self.which.set(3)
else:
self.which.set(5)
a = self.questions[self.which.get()]
self.question.config(text=a)
self.option.set(2)
def on_reset(self):
self.which.set(0)
self.option.set(2)
a = self.questions[self.which.get()]
self.question.config(text=a)
def on_close(self):
self.master.destroy()
if __name__ == '__main__':
app = App()
app.mainloop()
I am trying to evangelize toward an oo approach... so look at the below script
import tkinter as tk
class App(tk.Frame):
def __init__(self,):
super().__init__()
self.master.title("Hello World")
self.option =tk.IntVar()
self.option.set(2)
self.questions = 0:"Do you like apples?",
1:"How about peaches?",
2:"I like peaches too.",
3:"Have you tried mango?",
4:"Great",
5:"End of the questions."
self.which = tk.IntVar()
self.which.set(0)
self.init_ui()
def init_ui(self):
self.pack(fill=tk.BOTH, expand=1,)
f = tk.Frame()
self.question = tk.Label(f, text = self.questions[0])
self.question.pack()
ops = ('Yes','No',)
for index, text in enumerate(ops):
tk.Radiobutton(f,
text=text,
variable=self.option,
command=self.callback,
value=index,).pack(anchor=tk.W)
w = tk.Frame()
tk.Button(w, text="Print", command=self.callback).pack()
tk.Button(w, text="Reset", command=self.on_reset).pack()
tk.Button(w, text="Close", command=self.on_close).pack()
f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)
def callback(self):
#print(self.option.get())
if self.which.get()==0:
if self.option.get() == 0:
self.which.set(4)
else:
self.which.set(1)
elif self.which.get()==1:
if self.option.get() == 0:
self.which.set(2)
else:
self.which.set(3)
else:
self.which.set(5)
a = self.questions[self.which.get()]
self.question.config(text=a)
self.option.set(2)
def on_reset(self):
self.which.set(0)
self.option.set(2)
a = self.questions[self.which.get()]
self.question.config(text=a)
def on_close(self):
self.master.destroy()
if __name__ == '__main__':
app = App()
app.mainloop()
answered Mar 24 at 8:05
1966bc1966bc
2226
2226
This answer would be much better if you added an explanation. Otherwise we have to ready your code line-by-line and character-by-character to see how it's different from the code in the question.
– Bryan Oakley
Mar 24 at 13:13
add a comment |
This answer would be much better if you added an explanation. Otherwise we have to ready your code line-by-line and character-by-character to see how it's different from the code in the question.
– Bryan Oakley
Mar 24 at 13:13
This answer would be much better if you added an explanation. Otherwise we have to ready your code line-by-line and character-by-character to see how it's different from the code in the question.
– Bryan Oakley
Mar 24 at 13:13
This answer would be much better if you added an explanation. Otherwise we have to ready your code line-by-line and character-by-character to see how it's different from the code in the question.
– Bryan Oakley
Mar 24 at 13:13
add a comment |
Thanks for taking the time to reply. I tried your option and I wasn't able to make it work somehow.`
I tried to insert the nested if statements within the def Enter_presssed(event) code-block and it works up to a point. It recognises the first "if" and the last "else" but not the ifs and elses inbetween.
Shouldn't the program see and use each new input_get after Return is pressed as an answer to the last question displayed in the Text widget?
add a comment |
Thanks for taking the time to reply. I tried your option and I wasn't able to make it work somehow.`
I tried to insert the nested if statements within the def Enter_presssed(event) code-block and it works up to a point. It recognises the first "if" and the last "else" but not the ifs and elses inbetween.
Shouldn't the program see and use each new input_get after Return is pressed as an answer to the last question displayed in the Text widget?
add a comment |
Thanks for taking the time to reply. I tried your option and I wasn't able to make it work somehow.`
I tried to insert the nested if statements within the def Enter_presssed(event) code-block and it works up to a point. It recognises the first "if" and the last "else" but not the ifs and elses inbetween.
Shouldn't the program see and use each new input_get after Return is pressed as an answer to the last question displayed in the Text widget?
Thanks for taking the time to reply. I tried your option and I wasn't able to make it work somehow.`
I tried to insert the nested if statements within the def Enter_presssed(event) code-block and it works up to a point. It recognises the first "if" and the last "else" but not the ifs and elses inbetween.
Shouldn't the program see and use each new input_get after Return is pressed as an answer to the last question displayed in the Text widget?
answered Mar 28 at 7:52
ViktorViktor
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55321555%2fhow-to-use-the-last-user-input-in-text-widget-in-an-if-else-statement-after-ente%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown