0

I have a working program for sorting some screws using computer vision. Now I want to make a user-interface using tkinter. I am trying to keep the tkinter file and my program file separate, but I have problems with making my user inputs work.

As you see below i have a input function where the user can write "run" or "not" in the console to make the program start:

#This is just a part of my function in the program file
while True:
    #Ask if user will run the program
    yesOrNo = input('\nrun or not\n')
    if yesOrNo == 'run':
        break
    elif yesOrNo == 'not':
        print('programmet stoppes')
        quit()
    else:
        print('use only "run" or "not"')

The trouble begins when i call my function in the tkinter file using a button. Because when I have started the function I don't know how to interact with the function. I have tried using an Entry() and then replace my input() with entry.get(), but this crashes my tkinter, because the entry.get(), doesn't wait for the user to write something and therefore keeps running the while loop.

So summed up I just want something that acts like the input() and waits for the user to click enter or a button and then my function has to get this value while running (therefore I can't put the value as an parameter in the function). I want the input field to be inside my tkinter root and therefore I don't want to use tkinter.simpledialog.askstring().

I am not good at global variables, but is that a way to go? Like making a global variable inside my tkinter file which somehow can be read by my function (from the other file) while running. And then changing the value of the variable with a button or so.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • tkinter requires doing event-driven programming — so there's no "waiting for input". See [Introduction to GUI programming with `tkinter`](https://python-textbok.readthedocs.io/en/1.0/Introduction_to_GUI_Programming.html). – martineau Dec 22 '20 at 19:12
  • Also see @Bryan Oakley's answer to the question [Tkinter — executing functions over time](https://stackoverflow.com/questions/9342757/tkinter-executing-functions-over-time). – martineau Dec 22 '20 at 19:21
  • Thanks, that helped to understand tkinter a little better. Would you recommend, that i maybe split up my program in more functions. Like each time i have a user input? – Thor Iversen Dec 22 '20 at 20:24
  • You're still thinking procedurally. While a number of tkinter widgets support an optional user-supplied "callback" function that will get called when certain things happen — like whenever a `Button` is clicked, `Entry` widgets don't. However you can arrange for one of your own functions to be called when certain things occur, such as when character is inserted or deleted from one — this is known as "validation". Describing how to do it is too complicated to put into a comment. – martineau Dec 22 '20 at 21:32
  • Here's a little [documentation](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/entry-validation.html) to get you started, the topic is probably covered in many tkinter tutorials, and of course there's a number of questions about here on SO as well. – martineau Dec 22 '20 at 21:34
  • That's a great document:) Thanks a lot for your help! – Thor Iversen Dec 22 '20 at 21:43
  • As you said, you can use an `Entry` or `Radiobutton` to get the user input on `run` or `not`. Then you can pass it as an argument to your program file instead of using `input()`. – acw1668 Dec 23 '20 at 06:15

0 Answers0