Python 3-Tkinter,未按按钮就运行程序时弹出MessageBox

问题描述 投票:0回答:1

我有一个小问题,我找不到原因。我具有以下GUI,即使运行在仅按按钮即可触发的过程中,它也会在运行时弹出消息框。

甚至尝试创建一个仅显示消息框但仍无法解决问题的辅助功能。

谢谢您的帮助...我很确定有一个简单的修复程序,我只是看不到...

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()
python-3.x tkinter python-3.5 tkmessagebox
1个回答
0
投票
run_process = tk.Button(root, text="Generate JLs", width=15, command=run()).grid(row=4, column=1)

这不正确。

我以前对此感到困惑。您应该使用:

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

在python中,function是一个对象,调用函数应使用function()

如果调试此代码,则在调试此代码后会发现

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

您会发现它将调用run函数并运行它。最后,run_process["command"]将是run()的返回值

© www.soinside.com 2019 - 2024. All rights reserved.