0

I'm trying to make a menu driven program that has GUI capability. For the sake of this post I edited it down so the menu has 2 options, to quit and to calculate an average of a set of numbers. I ran this before I used the GUI code and it worked perfectly.

The issue is that when I click "Average", it runs the function and allows 1 input but then I can't enter another number. Same issue with other functions I had. They won't run past the first input and then the Quit button also won't quit.

Feel free to ask any questions in the comments if I wasn't good at describing what I needed. It's my first day using GUI so sorry if anything looks weird, I'm trying to teach myself but got stuck here. End goal is to have the whole program run in a GUI environment but I figured starting with the menu would be the best place to start.

from tkinter import *
import tkinter as tk

def average():
    print("Enter test scores to get the average of. Type '-99' to quit.")
    num = 1
    count = 0
    total = 0
    while num != -99:
        num = int(input("Enter numbers: "))
        total += num
        count += 1

    average = (total + 99) / (count-1)
    print("The average is: ", average)

    print("-----------")
    main()

def main():

    r = tk.Tk() 

    r.title('Number games')
    button1 = tk.Button(r, activebackground = "blue", bg = "light blue", text='Average', width=25, command= average)
    button4 = tk.Button(r, activebackground = "blue", bg = "light blue", text='Quit', width=25, command=r.destroy)

    button1.pack()
    button4.pack()

    r.mainloop()

main()
martineau
  • 119,623
  • 25
  • 170
  • 301
ricecake
  • 11
  • 2
  • You could run that same loop in the terminal. Not sure it's a tkinter issue... Most GUI apps would open an input box, not require to input somewhere else outside that GUI – OneCricketeer Dec 07 '19 at 03:27
  • Suggest you read [Tkinter — executing functions over time](https://stackoverflow.com/questions/9342757/tkinter-executing-functions-over-time) – martineau Dec 07 '19 at 03:36
  • `import *` is not prefered - see [PEP 8 -- Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/) – furas Dec 07 '19 at 03:38
  • The code you posted does not cause the problems you describe when I run it. It lets me enter multiple numbers, gives me the average, and the buttons remain responsive. I was going to say "you need to put the console input in its own thread", but for some reason I don't understand, your code seems to work fine as is. Of course, running `main()` from the end of `average()` repeats the same thing as when it is first run. – Dan Getz Dec 07 '19 at 03:40
  • your code works for me too. Linux Mint 19.2, Python 3.7.5. But maybe problem is only on some systems. For example PyGame on Windows has problem when it doesn't get events from system and in your program `mainloop()` is geting events from system but you `while` loop is blocking `mainloop` so it can't run. If you have long-running code then you should rather run it in separated thread. – furas Dec 07 '19 at 03:43
  • frankly, GUI which uses `input()` instead of `tk.Entry()` to get value looks very weird. – furas Dec 07 '19 at 03:46
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759), [Best way to structure a tkinter application](https://stackoverflow.com/a/17470842/7414759) and your error [While Loop Locks Application](https://stackoverflow.com/questions/28639228/python-while-loop-locks-application) – stovfl Dec 07 '19 at 08:03

2 Answers2

2

You don't need to call main() from inside average(). When I delete that line, your program works exactly as expected for me (debian 9, python3).

Ps. Don't use import *.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

Globalize the variable r and declare r.destroy() in another function above main function. Note from tkinter import * is enough no need to import tkinter again with an alias.

viruchith
  • 51
  • 1
  • 8