【问题标题】:Python 3 - Tkinter, MessageBox popsup when program is run not on button press [duplicate]Python 3 - Tkinter,当程序运行时弹出消息框而不是按下按钮[重复]
【发布时间】:2020-06-15 18:16:43
【问题描述】:

我有一个小问题,我找不到原因。 我有以下 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】:
    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() 的返回值

    【讨论】:

    • 解决了,括号......谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-12-23
    • 2020-06-17
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多