LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2022, 12:00 PM   #1
IamtheSenate
LQ Newbie
 
Registered: Feb 2021
Location: Manchester
Distribution: Fedora 38 Xfce and Linux Mint 21.2 Xfce
Posts: 11

Rep: Reputation: Disabled
Text field in Tkinter returns not defined (Python)


I'm using the Python Tkinter code below as a 'simple' demo. I'm still very new to Tkinter but I can't figure this out after several hours:

Code:
import tkinter as tk

login = tk.Tk()
login.geometry('300x150')
login.title('Login')

# login_screen entry fields
utext = tk.StringVar()
ptext = tk.StringVar()

# diary entry fields
date = tk.StringVar()
subject = tk.StringVar()

def login_process():
    if utext.get() == "u" and ptext.get() == "p":
        login.destroy()
        diary()
    
    else:
        tk.Label(login, text="Username or password wrong").pack()

def save():
    print(journal_entry.get("1.0","end"))

def login_screen():
    # username label and entry
    username_label = tk.Label(text="Enter username:").pack()
    username_entry = tk.Entry(textvariable=utext, fg="green", bg="black", width=15).pack()

    # password label and entry
    password_label = tk.Label(text="Enter password:").pack()
    password_entry = tk.Entry(textvariable=ptext, fg="green", bg="black", width=15, show="*").pack()

    #submit button
    submit = tk.Button(text="Submit", width=10, height=1, bg="red", fg="white", command=login_process).pack()

    login.mainloop()

def diary():
    journal = tk.Tk()
    journal.geometry('700x400')
    journal.title('My Journal')

    # journal date
    date_label = tk.Label(text="Today's date (DD.MM.YY):").pack()
    date_entry = tk.Entry(textvariable=date, fg="black", bg="white", width=10).pack()

    # journal subject
    subject_label = tk.Label(text="Subject:").pack()
    subject_entry = tk.Entry(textvariable=subject, fg="black", bg="white", width=40).pack()

    # journal entry
    journal_label = tk.Label(text="Journal entry:").pack()
    journal_entry = tk.Text(journal, height=10, width=40)
    journal_entry.pack()

    # save button
    submit = tk.Button(text="Submit", width=10, height=1, bg="red", fg="white", command=save).pack()

    journal.mainloop()

login_screen()
The purpose is to log in, create a journal entry and then save as a text file. However the text field variable name is always returned as 'undefined':

Code:
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/home/aaron/Desktop/scripts_for_tuesday/test.py", line 24, in save
    print(journal_entry.get("1.0","end"))
NameError: name 'journal_entry' is not defined
 
Old 05-08-2022, 03:15 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
The way you have that written, you could solve that with

Line 54
Code:
# journal entry
journal_label = tk.Label(text="Journal entry:").pack()
global journal_entry
journal_entry = tk.Text(journal, height=10, width=40)
journal_entry.pack()
Or return something from the function.
Code:
def diary():
Another words. Function def save(): is not aware of anything in Function def diary(): unless you share it.

Edit:
And I did run that. And look for error.
https://0x0.st/omcr.png
https://0x0.st/omcs.png

Last edited by teckk; 05-08-2022 at 03:18 PM.
 
2 members found this post helpful.
Old 05-08-2022, 04:49 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Code:
#!/usr/bin/python

#Share variables between functions demo

def drawLine(length, label=''):
    line = '-' * length
    if label:
        line += ' ' + label
    print(line)

def interval(num):
    if num > 0:
        interval(num - 1)
        drawLine(num)
        interval(num - 1)

def drawRuler(inches, length):
    drawLine(length, '0')
    for i in range(1, 1 + inches):
        interval(length - 1)
        drawLine(length, str(i))

drawRuler(5, 3)
 
1 members found this post helpful.
Old 05-09-2022, 03:02 AM   #4
IamtheSenate
LQ Newbie
 
Registered: Feb 2021
Location: Manchester
Distribution: Fedora 38 Xfce and Linux Mint 21.2 Xfce
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
The way you have that written, you could solve that with

Line 54
Code:
# journal entry
journal_label = tk.Label(text="Journal entry:").pack()
global journal_entry
journal_entry = tk.Text(journal, height=10, width=40)
journal_entry.pack()
Or return something from the function.
Code:
def diary():
Another words. Function def save(): is not aware of anything in Function def diary(): unless you share it.

Edit:
And I did run that. And look for error.
https://0x0.st/omcr.png
https://0x0.st/omcs.png
Thank you very much, after all that debugging and decomposition of the program it was just an out of scope variable!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python : display content file in Text widget tkinter with update atlass218 Programming 0 12-16-2018 02:45 PM
Xombrero: can't see text entered in address field and search field xiongnu Linux - Software 1 01-23-2016 08:20 AM
Text widget in Tkinter(Python on Win 7) not getting updated simultaneously!!! B Akshay Programming 1 02-08-2013 10:49 AM
python, tkinter, command is executed when program is run not when button clicked deathalele Programming 5 06-15-2009 03:13 PM
TKInter, Python and A Pack Of Crisps Odd_Bloke Linux - Software 3 08-25-2003 04:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration