【问题标题】:how to link a function with button to an entry box using python gui?如何使用python gui将带有按钮的函数链接到输入框?
【发布时间】:2019-05-26 21:45:34
【问题描述】:

please click here to see the image.我想将我的 python 函数与输入框链接起来。假设我在 gui 中的按钮上添加一个函数。如果我单击按钮,则函数输出应显示在 python 输入框上。但我在 python shell 中获取输出。请帮我解决这个问题。
@Jundullah output of your codeimage1
output is replaceing with new input, insteed i need to print beside.image2

****这里是代码****

from tkinter import *

root = Tk()

text_input = StringVar()

operator = ""

txtDisplay= Entry(font=('arial',70,'bold italic'),textvariable=text_input , 
                  bd=30,insertwidth=4,bg="#b0e0e6", justify = "center")

root.title("Alphabet Pattern")

txtDisplay.grid(columnspan=10)

def A():

    """This function will print alphabet A"""

    for i in range(7):

        for j in range(7):

            if ((j==0 or j==6)) and i!=0 or ((i==0 or i==3) and (j>0 and 
                                                                     j<6)):
                print("*",end="")

            else:

                print(end=" ")

        print()

    print()

def C():

    """This function will print alphabet C"""

    for row in range(7):

        for col in range(7):

            if (col==0 and (row!=0 and row!=6) or ((row==0 or row==6) and 
                                                                (col>0))):

                print("*",end="");

            else:

                print(end=" ")

        print()

    print()

#first button

A1 = Button(padx=16,pady=16,bd=8,fg="#000000",font=('arial',30,'bold 
italic'),text="A",bg="#b0e0e6e",command= A ).grid(row=3, column=0)

#second button

C1 = Button(padx=16,pady=16,bd=8,fg="#000000",font=('arial',30,'bold 
italic'),text="C",bg="#b0e0e6",command=  C).grid(row=4, column=2)

root.mainloop()

请给我任何解决上述代码的建议。

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    用这个代替 print 函数:

    使用这个:

    var = Label(padx=32,pady=32,fg="#000000",font=('arial',30,'bold 
              italic'),text="something",bg="#b0e0e6").grid(row=4, column=2)
    

    它有效,但需要整洁! :)

    你应该试试:

    from tkinter import *
    
    root = Tk()
    
    text_input = StringVar()
    
    operator = ""
    
    txtDisplay= Entry(font=('arial',70,'bold italic'),textvariable=text_input , 
    bd=30,insertwidth=4,bg="#b0e0e6", justify = "center")
    
    root.title("Alphabet Pattern")
    
    txtDisplay.grid(columnspan=10)
    
    c = ""
    a = ""
    
    def A():
        global a
        a = ""
        """This function will print alphabet A"""
    
        for i in range(7):
    
            for j in range(7):
    
                if ((j==0 or j==6)) and i!=0 or ((i==0 or i==3) and (j>0 and j<6)):
                    a += "*"
    
                else:
                    a += " "
            a += "\n"
    
        var["text"] = a
    
    def C():
        global c
        c = ""
        """This function will print alphabet C"""
    
        for row in range(7):
    
            for col in range(7):
    
                if (col==0 and (row!=0 and row!=6) or ((row==0 or row==6) and 
    (col>0))):
                    c += "*"
                else:
                    c += " "
            c += "\n"
    
        var["text"] = c
    
    var = Label(root)
    var.config(text="Allah",bd=0,fg="orange",width=0,height=0,bg="#000000",font= 
                                                             ("courier new",40))
    var.place(relx=1,x=-500, y=250, anchor=W)
    
    #first button
    
    A1 = Button(padx=16,pady=16,bd=8,fg="#000000",font=('arial',30,'bold 
    italic'),text="A",bg="#0f0f0f",command= A ).grid(row=3, column=0)
    
    #second button
    
    C1 = Button(padx=16,pady=16,bd=8,fg="#000000",font=('arial',30,'bold 
    italic'),text="C",bg="#0f0f0f",command=  C).grid(row=4, column=2)
    
    root.mainloop()
    

    【讨论】:

    • 您的答案非常接近问题。但问题是输入框中没有打印输出,在上面的代码中,您创建了一个新的小部件来打印输出。第二个问题是输出正在替换的小部件。 我无法将屏幕截图上传到此评论,因此将在我的问题中上传屏幕截图,请参考我的问题。谢谢@Jundullah
    • 参考问题,我已经给了链接
    • @sukesh 也许可以解决这个问题:stackoverflow.com/a/16374273/10696411
    • 是的,但是在函数中我编写了模式逻辑,当我单击按钮模式输出无法在输入框中打印时,如果我更改我的功能(模式为正常句子)它正在工作。我已经尝试过你所说的逻辑,但我没有得到所需的输出。 @Jundullah
    • 您发送的图片链接无效。我要见他们!
    猜你喜欢
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 2017-01-10
    相关资源
    最近更新 更多