如何进入一个字符串,然后进入一个输入,这样我可以将4个条目放入一个公式?

时间:2016-03-06 00:40:05

标签: python tkinter

  • 我只需要将一个条目转换为一个字符串,然后我可以将该字符串转换为整数或浮点数。感谢

  • 我试图用括号做出奇怪的事情,但似乎没有任何效果。对不起,我没有把代码放进去,但我不太清楚如何在这里快速传递代码。

  • 我需要输入3个,4个,5个和6个条目,然后是整数还是浮点数

    root.title("Exemption?")  
    root.geometry("400x200")  
    root["bg"] = "green"  
    
    def pn(a,b,c,d):  
            return (720-2*d-a-b-2*c)/2  
    
    label1 = Label(root, text = "Name:")  
    label2 = Label(root, text = "Subject:")  
    label3 = Label(root, text = "Average 1:")  
    label4 = Label(root, text = "Average 2:")  
    label5 = Label(root, text = "Average 3:")  
    label6 = Label(root, text = "Average 4:")  
    
    entry1 = Entry(root)  
    entry2 = Entry(root)  
    a = entry3 = Entry(root)  
    b = entry4 = Entry(root)  
    c = entry5 = Entry(root)  
    d = entry6 = Entry(root)  
    
    button = Button(root, text="Average needed", command= pn)  
    
    label1.grid(row=1,column=1)  
    label2.grid(row=2,column=1)  
    label3.grid(row=3,column=1)  
    label4.grid(row=4,column=1)  
    label5.grid(row=5,column=1)  
    label6.grid(row=6,column=1)  
    
    entry1.grid(row=1,column=2)  
    entry2.grid(row=2,column=2)  
    entry3.grid(row=3,column=2)  
    entry4.grid(row=4,column=2)  
    entry5.grid(row=5,column=2)  
    entry6.grid(row=6,column=2)  
    
    button.grid(row=7,column=2)  
    
    root.mainloop()  
    

1 个答案:

答案 0 :(得分:0)

您应该使用StringVar()来创建变量:

from Tkinter import *

root = Tk()
root.title("TkApp")

var = StringVar()

def varWriter(*args):
    print var.get()
    print type(var.get())
    print int(var.get())
    print type(int(var.get()))
    print float(var.get())
    print type(float(var.get()))
var.trace("w", varWriter)

label = Label(root, textvariable=var)
label.pack()

text_entry = Entry(root, textvariable=var)
text_entry.pack()

root.mainloop()

但是你必须小心任意改变源数据类型。

More information

修改

根据OP的要求:

from Tkinter import *

root = Tk()

root.title("Exemption?")

root.geometry("400x200")

root["bg"] = "green"

a = StringVar()
b = StringVar()
c = StringVar()
d = StringVar()

int_a = 0
int_b = 0
int_c = 0
int_d = 0

def pn(int_a, int_b, int_c, int_d):
        print (720 - 2 * int_d - int_a - int_b - 2 * int_c ) / 2

def aWriter(*args):
    global int_a
    int_a = int(a.get())

def bWriter(*args):
    global int_b
    int_b = int(b.get())

def cWriter(*args):
    global int_c
    int_c = int(c.get())

def dWriter(*args):
    global int_d
    int_d = int(d.get())

a.trace("w", aWriter)
b.trace("w", bWriter)
c.trace("w", cWriter)
d.trace("w", dWriter)

label1 = Label(root, text = "Name:")

label2 = Label(root, text = "Subject:")

label3 = Label(root, text = "Average 1:", textvariable=a)

label4 = Label(root, text = "Average 2:", textvariable=b)

label5 = Label(root, text = "Average 3:", textvariable=c)

label6 = Label(root, text = "Average 4:", textvariable=d)

entry1 = Entry(root)

entry2 = Entry(root)

entry3 = Entry(root, textvariable=a)

entry4 = Entry(root, textvariable=b)

entry5 = Entry(root, textvariable=c)

entry6 = Entry(root, textvariable=d)

button = Button(root, text="Average needed", command=pn(int_a,int_b,int_c,int_d))

label1.grid(row=1,column=1)

label2.grid(row=2,column=1)

label3.grid(row=3,column=1)

label4.grid(row=4,column=1)

label5.grid(row=5,column=1)

label6.grid(row=6,column=1)

entry1.grid(row=1,column=2)

entry2.grid(row=2,column=2)

entry3.grid(row=3,column=2)

entry4.grid(row=4,column=2)

entry5.grid(row=5,column=2)

entry6.grid(row=6,column=2)

button.grid(row=7,column=2)

root.mainloop()