如何使用python gui将带有按钮的功能链接到输入框?

时间:2018-12-29 11:36:45

标签: python tkinter

please click here to see the image.我想将我的python函数与输入框链接。假设我向gui中的按钮添加了函数。如果我单击按钮,则函数输出应显示在python输入框中。但是我正在python shell中获取输出。请帮助我。
                                                                           @Jundullah output of your code 图片1
output is replaceing with new input, insteed i need to print beside. 图片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)

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()

请给我一些解决以上代码的建议。

1 个答案:

答案 0 :(得分:0)

使用此功能代替 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()