TAGS :Viewed: 14 - Published at: a few seconds ago

[ Python tkinter entry widget ]

I am unable to fill entry widget by values of Vfrat, V_base, floss, and constloss. Could anyone let me know how to fill values once program is executed.

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

I am unable to fill entry widget by values of Vfrat, V_base, floss, and constloss. Could anyone let me know how to fill values once program is executed.

Answer 1


The entry widget is normally used to accept user input. That's what the 'textvariable' parameter is for. You need to set it to a tkinter variable (e.g. StringVar) which will then hold the value in the entry widget box. In your case, just set the variable's default value with its 'set' method.

Here's an update print_out function that shows how to do this. You can later change the type of variable from StringVar to something more suited to your data. Note I also moved the mainloop() into this function and changed the x values for some of the overlapping widgets.

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

Don't forget to call this print_out function after getting your values, which as coded will only show the tkinter window after the graph is displayed and closed.

if __name__ == '__main__':

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

This is a start. For a full GUI app you could use another entry widget to get the step-of-frequency from the user and then add a button to call the two functions.

Answer 2


If you want to insert a value into an Entry widget, you must have a reference to the widget, and you can call the insert method.

To have a proper reference you must not call place on the same line that you call Button. Define your button like this:

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

Later, you can insert something into the widget with the insert method:

mentry1.insert(0, Vfrat)