I having trouble running my python Tkinter question

158 Views Asked by At

I'm working on a python assignment and this is where I got so far. I'm stuck and cannot execute the application. I'm making a calculator that scores the average and gives a grade letter. I was looking into my professor's video and there was "import tkinter.messagebox as tkm" but Im not sure how to implement this in the code.

this is my code:

import tkinter as tk
import tkinter.messagebox as tkm

window = tk.Tk()
window.geometry('400x400')
window.title("Exam Calculator")

window = tk.Tk()
window.geometry('300x300')

def calculate():
    score1 = float(entry1.get())
    score2 = float(entry2.get())
    score3 = float(entry3.get())
    avg = (score1 + score2 + score3)/3
    if(avg>=90):
        lettergrade= "A"
    elif(avg>=80 and avg<=89):
        lettergrade = "B"
    elif(avg>=70 and avg<=79):
        lettergrade= "C"
    elif(avg>=60 and avg<=69):
        lettergrade = "D"
    else:
        lettergrade = "F"


label1 = tk.Label(window, text='Test 1')
label1.pack()

entry1 = tk.Entry(window)
entry1.pack()

label2 = tk.Label(window, text='Test 2')
label2.pack()

entry2 = tk.Entry(window)
entry2.pack()

label3 = tk.Label(window, text='Test 3')
label3.pack()

entry3 = tk.Entry(window)
entry3.pack()

button2 = tk.Button(window, text="Calculate", 
command=calculate)
Button1 = tk.Button(window, text="quit", 
command=window.destroy)
2

There are 2 best solutions below

0
On

There are multiple problems in your code. First, you define window two times. The second time, you just override your frist instance of window, so just leave that out. Then you are not packing your Buttons, which means they will not be shown in your window. Lastly, you are missing the most important part of your Tkinter Application, which is to start the applications mainloop, which makes the window pop up and tells Tkinter to start listening for your mouse and keyboard interaction with the window and do something with it. This is called an event loop and is the main component of every graphical user interface. You start the eventloop by calling .mainloop() on your instance of tk.Tk, which is your window variable.

Lastly, it is unclear from your text what you actually want to do with the Messagebox. I assume that you want to use the message box to display the result of your calculate function, since right now it doesn't do anything.

import tkinter as tk
import tkinter.messagebox as tkm

window = tk.Tk()
window.geometry('400x400')
window.title("Exam Calculator")

def calculate():
    score1 = float(entry1.get())
    score2 = float(entry2.get())
    score3 = float(entry3.get())
    avg = (score1 + score2 + score3)/3
    if(avg>=90):
        lettergrade= "A"
    elif(avg>=80 and avg<=89):
        lettergrade = "B"
    elif(avg>=70 and avg<=79):
        lettergrade= "C"
    elif(avg>=60 and avg<=69):
        lettergrade = "D"
    else:
        lettergrade = "F"
    message = 'Your result is ' + lettergrade
    tkm.showinfo(title='Result', message=message)


label1 = tk.Label(window, text='Test 1')
label1.pack()

entry1 = tk.Entry(window)
entry1.pack()

label2 = tk.Label(window, text='Test 2')
label2.pack()

entry2 = tk.Entry(window)
entry2.pack()

label3 = tk.Label(window, text='Test 3')
label3.pack()

entry3 = tk.Entry(window)
entry3.pack()

button2 = tk.Button(window, text="Calculate", 
command=calculate)
button2.pack()
Button1 = tk.Button(window, text="quit", 
command=window.destroy)
Button1.pack()

window.mainloop()
0
On

messagebox can help to create fast small message windows. The usage is very simple, just implement this in your code:

from tkinter import messagebox

In you case:

from tkinter import messagebox as tkm

Then:

messagebox.function(title,message,options)

In your case:

tkm.function(title,message,options)

The functions are:

  • showinfo(): for showing some relevant informations.
  • showwarning(): for displaying a warning to the user.
  • showerror(): for displaying an error message.
  • askquestion(): for asking a yes/no question to the user.
  • askokcancel(): confirm the user’s action regarding some application activity.
  • askyesno(): for asking a yes/no question about a user action.
  • askretrycancel(): for asking the user about doing a specific task again.

The options are:

  • default: this option is used to specify the default button like ABORT, RETRY, or IGNORE in the message box.
  • parent: this option is used to specify the window on top of which the message box is to be displayed.

The code needs just some improvements:

  • pack() the two buttons (as to display them)
  • add window.mainloop() at the end of your code (this is why is does not start)