python-3.xtkinterpython-3.5tkmessagebox

Python 3 - Tkinter, MessageBox popsup when program is run not on button press


I have a small issue for which I can`t find the reason. I have the following GUI, and it pops the message box when I run it, even though it is inside a procedure which is triggered only on button press.

Tried even creating a secondary function which will show only the message box and still did not fix the issue.

Thank you for your help... I am quite sure that there is an easy fix which I just do not see...

import tkinter as tk
from tkinter import ttk
import tkinter.messagebox
import jl_generator


def run():
    jl_generator.run_process()
    tkinter.messagebox.showerror('Done','Done')

def show():
    temp_list = user_input_list
    for i in range(0, len(user_input_list[0])):
        listBox.insert("", "end", values = (user_input_list[0][i],user_input_list[1][i],user_input_list[2][i],user_input_list[3][i],user_input_list[4][i],user_input_list[6][i],user_input_list[8][i]))


# Column Names for the TreeView
cols = ('Entity', 'Customer Nr', 'Account Code', 'Profit Centre', 'Partner Profit Centre', 'Amount', 'Nr Of Journal Lines')
# Input data for the tree view
user_input_list, journal_code = jl_generator.get_user_input()

#Creating the
root = tk.Tk()
root.title('JL Generator')

#Create the treeview
listBox = ttk.Treeview(root, columns=cols, show='headings')
for col in cols:
    listBox.heading(col, text=col)
listBox.grid(row=1, column=0, columnspan=3)

#-------------LABELS--------------
#Title Label
label = tk.Label(root, text="Journal Lines Generator", font=("Arial",30)).grid(row=0, columnspan=3)
#Journal Code Label
show_journal_code = tk.Label(root, text = 'Journal Code = ' + journal_code).grid(row=6, column=1)
#Number of Journal Lines Label
show_number_of_journal_lines = tk.Label(root, text = 'Number of Journal Lines = ' + str(sum(user_input_list[8][i] for i in range(0, len(user_input_list[0]))))).grid(row=5, column=1)

#------------BUTTONS-----------
#Run the Generation
run_process = tk.Button(root, text="Generate JLs", width=15, command=run()).grid(row=4, column=1)
#Show the input data
showScores = tk.Button(root, text="Show Input", width=15, command=show).grid(row=4, column=0)
#Close the window
closeButton = tk.Button(root, text="Exit", width=15, command=exit).grid(row=4, column=2)


root.mainloop()

Solution

  • run_process = tk.Button(root, text="Generate JLs", width=15, command=run()).grid(row=4, column=1)
    

    this is incorrect.

    I used to feel confused about this. You should use:

    run_process = tk.Button(root, text="Generate JLs", width=15, command=run).grid(row=4, column=1)
    

    In python,function is an object,call function should use function()

    If you debug this code,you will find that after debug this code

    run_process = tk.Button(root, text="Generate JLs", width=15, command=run()).grid(row=4, column=1)
    

    you will find it will call run function and run it. And Finally,run_process["command"] will be the returned value ofrun()