1

I'm using a tkinter Entry widget in a temporary window to collect information from a user. I want to be able to use that information for a variety of things: rewrite it in a Label in another window, convert it to an integer for calculations, get its value as a string for writing to a file, and so on. In this stripped down code, I've got two text variables, and I've used quotation marks to name one and not the other. As I understand it, using the quotation marks is wrong--the variable without the quotation marks works correctly with get, for example. The trouble is that the one without the quotation marks doesn't copy correctly when I use it as the textvariable for a Label in a different window. What am I missing here?

from tkinter import *
from tkinter import ttk

root = Tk()

sheetid = StringVar
sheetname = StringVar

def dosheet():
    sheet = Toplevel(root)
    ttk.Label(sheet, text="Sheet ID:").grid(row=0)
    ttk.Label(sheet, textvariable=sheetid, font=(11)).grid(row=1)
    ttk.Label(sheet, text="Sheet Name:").grid(row=2)
    ttk.Label(sheet, textvariable="sheetname", font=(11)).grid(row=3)
#end dosheet

def new():
    def finish():
        start.destroy()
        dosheet()

    start = Toplevel(root)
    start.title("New sheet dialog")
    ttk.Label(start, text="Sheet ID:").grid(row=1)
    ttk.Entry(start, textvariable=sheetid).grid(row=2)
    ttk.Label(start, text="Sheet name:").grid(row=3)
    ttk.Entry(start, textvariable="sheetname").grid(row=4)
    ttk.Button(start, text="Close", command=finish).grid(row=5)
#end new

main = ttk.Frame(root, padding="16 12 12 12")
main.grid()

ttk.Label(main, text="Start new sheet:").grid()
ttk.Button(main, text="New", command=new).grid()

root.mainloop()
Glenn Ledder
  • 21
  • 1
  • 4
  • 1
    What do you mean when you say *"The trouble is that the one without the quotation marks doesn't copy correctly when I use it as the textvariable for a Label in a different window."*? The word "copy" doesn't make sense in this context. And what do you mean when you say it is not correct? Are the letters jumbled? Are some letters missing? – Bryan Oakley May 05 '13 at 18:35
  • I pushed the new button and entered numbers for the two Entry boxes. The Close button then creates a window that uses those two numbers. Then I pushed New again and typed in different numbers. When I push Close, the new window does not have any number for sheetid, but "sheetname" does match what I just typed into the Entry box. I either solved the problem or found a workaround. My new code uses Entry variables in1 and in2 and Label variables out1 and out2. The Close button uses sheetid=in1.get() and out1.set(sheetid). The variables have to be declared global in the function defs. – Glenn Ledder May 07 '13 at 13:24
  • 2
    Now I feel pretty stupid. sheetid=StringVar doesn't work, but sheetid=StringVar() does. First law of computer code: quote syntax verbatim even if you don't understand it. – Glenn Ledder May 07 '13 at 13:28
  • @GlennLedder I have elaborated my problem in this question :- http://stackoverflow.com/questions/27866581/how-to-use-stringvar-and-display-the-value-inserted-in-a-tkinter-entry Please looking for your suggestions. – sigdelsanjog Jan 09 '15 at 18:10

1 Answers1

0

What about this:

# with () you call the class to create a new object (instance) of it.
sheetid = StringVar('sheetid')
sheetname = StringVar('sheetname') 



def dosheet():
    #                             remove "
    ttk.Label(sheet, textvariable=sheetname, font=(11)).grid(row=3)

What does it do?

User
  • 14,131
  • 2
  • 40
  • 59