2015-12-02 187 views
0

我无法通过Vfrat,V_base,floss和constloss的值填充条目小部件。 任何人都可以让我知道如何填写值,一旦程序执行。Python tkinter条目小部件

import math 
import matplotlib.pyplot as plt 
import numpy as np 
from decimal import Decimal 
import sys 
from Tkinter import * 

def v_f_calculation(): 
    v_line = 28.0 
    f_base = 100.0 
    f_top = 200.0 
    f_step = input('Enter the no of step of frequency') 
    V_frat = v_line/f_base 
    print 'vfrat is ', V_frat 
    freq = np.arange(f_base/f_step, f_top+(f_base/f_step), f_base/f_step) 
    print 'frequency is ', freq 
    print('length of freq = ',len(freq)) 
    V_base = np.arange(f_base/f_step*V_frat, f_base*V_frat, f_base/f_step*V_frat) 
    print 'V_base is ', V_base 
    V_top = np.ones(len(freq)-len(V_base))*v_line 
    print 'V_top is = ', V_top 
    V_v_f = np.concatenate((V_base, V_top), axis = 0) 
    print 'V_v_f is = ',V_v_f 
    print 'length of V_V_F', len(V_v_f) 
    plt.figure(1) 
    plt.plot(freq, V_v_f, 'ro-') 
    plt.show() 
    return V_frat, V_base, V_top, V_v_f 


def get_fric_windage_loss(): 

    st_cr_lg = 120.0 
    f_base = 100.0 
    lg = 0.5 
    tot_iron_loss = 500.0 
    rt_od = 100.0 

    fric_loss = ((rt_od*10**(-3))*(st_cr_lg*(10**(-3)))*f_base**0.38)/(lg/1000) 
    cons_loss = fric_loss + tot_iron_loss 
    print 'friction loss is = ', fric_loss 
    print 'constant loss is = ', cons_loss 
    return fric_loss, cons_loss 

def print_out(vf, floss): 

    mgui = Tk() 
    text = StringVar() 
    wer = IntVar() 
    mgui.geometry ('1200x1200') 
    mgui.title('Output Sheet') 

    mlabel1 = Label(text='Vfrat', fg='black', bg = 'white').place(x=210, y=20) 
    mentry1 = Entry(textvariable = vf[0], width = 5).place(x=210, y =45) 

    mlabel2 = Label(text='V_base', fg='black', bg = 'white').place(x=270, y=20) 
    mentry2 = Entry(textvariable = vf[1], width = 5).place(x=270, y =45) 


    mlabel3 = Label(text='floss', fg='black', bg = 'white').place(x=270, y=20) 
    mentry3 = Entry(textvariable = floss[0], width = 5).place(x=270, y =45) 


    mlabel4 = Label(text='const_loss', fg='black', bg = 'white').place(x=270, y=20) 
    mentry4 = Entry(textvariable = floss[1], width = 5).place(x=270, y =45) 
    return 
mainloop() 
if __name__ == '__main__': 
    vf = v_f_calculation() 
    floss = get_fric_windage_loss() 

我无法通过Vfrat,V_base,floss和constloss的值填充条目窗口小部件。 任何人都可以让我知道如何填写值,一旦程序执行。

+0

什么是“无法”是什么意思?什么阻止你这样做?你有错误吗?什么错误? –

回答

0

入口小部件通常用于接受用户输入。这就是'textvariable'参数的用途。您需要将其设置为一个tkinter变量(例如StringVar),然后该变量将保存该条目窗口小部件框中的值。在你的情况下,只需使用其'set'方法设置变量的默认值即可。

这是一个更新print_out函数,显示了如何执行此操作。稍后可以将变量的类型从StringVar更改为更适合您数据的内容。注意我也将mainloop()移到了这个函数中,并且改变了一些重叠小部件的x值。

def print_out(vf, floss): 
    mgui = Tk() 
    text = StringVar() 
    var_vfrat = StringVar() 
    var_vbase = StringVar() 
    var_floss = StringVar() 
    var_const_loss = StringVar() 
    wer = IntVar() 

    var_vfrat.set(vf[0]) 
    var_vbase.set(vf[1]) 
    var_floss.set(floss[0]) 
    var_const_loss.set(floss[1]) 

    mgui.geometry('1200x1200') 
    mgui.title('Output Sheet') 

    mlabel1 = Label(text='Vfrat', fg='black', bg='white').place(x=210, y=20) 
    mentry1 = Entry(textvariable=var_vfrat, width=5).place(x=210, y=45) 

    mlabel2 = Label(text='V_base', fg='black', bg='white').place(x=270, y=20) 
    mentry2 = Entry(textvariable=var_vbase, width=5).place(x=270, y=45) 

    mlabel3 = Label(text='floss', fg='black', bg='white').place(x=330, y=20) 
    mentry3 = Entry(textvariable=var_floss, width=5).place(x=330, y=45) 

    mlabel4 = Label(text='const_loss', fg='black', bg='white').place(x=390, y=20) 
    mentry4 = Entry(textvariable=var_const_loss, width=5).place(x=390, y=45) 
    mgui.mainloop() 
    return 

不要忘记让你的价值观,后调用这个函数print_out作为其编码显示和关闭后的图形将只显示Tkinter的窗口。

if __name__ == '__main__': 

    vf = v_f_calculation() 
    floss = get_fric_windage_loss() 
    print_out(vf, floss) 

这是一个开始。对于完整的GUI应用程序,您可以使用另一个输入窗口小部件来获取用户的步频,然后添加一个按钮来调用这两个函数。

+0

re:_“入口小部件通常用于接受用户输入,这就是'textvariable'参数用于”_这不完全正确。确实,一个入口小部件用于接受用户输入,它与'textvariable'参数完全无关。该参数有几个用途,既获取值和显示值(并且不是严格要求) –

0

如果要将值插入到Entry小部件中,则必须具有对小部件的引用,并且可以调用insert方法。

要得到正确的参考,您不得在称为Button的同一行上拨打place。定义你的按钮,像这样:

mentry1 = Entry(textvariable=var_vfrat, width=5) 
... 
mentry1.place(x=210, y=45) 

后来,你可以插入一些到小工具与方法insert

mentry1.insert(0, Vfrat) 
+0

感谢Brad n Bryan .... :) –