weixin_42979701 2019-07-05 02:51 采纳率: 50%
浏览 9695
已采纳

python tkinter里面,我怎么把函数执行的结果显示(print)到创建的Text里面?

import tkinter as tk
root=tk.Tk()
root.title("how to do ")
root.geometry('500x300')
tk.Label(root, text="显示", bg="green", font=("Arial", 12), width=5, height=1).place(x=20,y=30)

def printtext():
    EditText.insert('1.0',A())
def A():
                if 2>3:print("句子1")
                elif 2<0:print("句子2")
                else:print("句子3")
EditText = tk.Text(root,width=20,height=10)   
EditText.grid(row=2,column=3)
btn_test=tk.Button(root, text="按钮", command =printtext,width=5, height=2)       

btn_test.place( x=200,y=60)

root.mainloop()

这样执行的结果为:tkinter.TclError: wrong # args: should be ".!text insert index chars ?tagList chars tagList ...?"

  • 写回答

1条回答 默认 最新

  • zf.xin 2019-07-05 09:31
    关注

    insert 方法第一个参数表示插入文本的位置,第二个参数表示要插入的文本。你的代码中方法A()没有返回值,正确代码如下

    import tkinter as tk
    root=tk.Tk()
    root.title("how to do ")
    root.geometry('500x300')
    tk.Label(root, text="显示", bg="green", font=("Arial", 12), width=5, height=1).place(x=20,y=30)
    
    def printtext():
        EditText.insert(1.0,A())
    def A():
        if 2>3:
            return "句子1"
        elif 2<0:
            return "句子2"
        else:
            return "句子3"
    
    EditText = tk.Text(root,width=20,height=10)
    EditText.grid(row=2,column=3)
    btn_test=tk.Button(root, text="按钮", command =printtext,width=5, height=2)
    
    btn_test.place( x=200,y=60)
    
    root.mainloop()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?