Home>

Always I am indebted.
This is a flow of entering a name on the login screen, calculating inventory on the calculation screen, and pressing the Back button to return to the first sheet. The second login doesn't work.
I would like to improve this so that I can log in and out correctly.
↓ Login screen (1st sheet)

↓ Calculation screen (2nd sheet)

After pressing the Back button to return to the login screen, even if you enter a different name in the entry box, it will be the same as the name you entered the first time.
Example) Enter "Tanaka" for the first time and press the Back button to return to the login screen and log out.
(This logout means that "Mr. Tanaka has left the room" is written in the history in a separate file called issue management.)
After that, even if I entered "Ariyoshi" and logged in, the file was written as "Tanaka" instead of "Ariyoshi".

#-*-coding: utf-8-*-
import tkinter
from tkinter import *
try: import tkinter as tk
except: import tkinter as tk
import datetime
window = tkinter.Tk ()
window.geometry ("1358x750")
window.title ("Screen Transition")
f1 = open ('magic red.txt')
f2 = open ('Magic Blue.txt')
f3 = open ('magic black.txt')
data1 = f1.read ()
data2 = f2.read ()
data3 = f3.read ()
f1.close ()
f2.close ()
f3.close ()
canvas1 = tkinter.Canvas (bd = 0, width = 1366, height = 768)
titleLabel_j = tk.Label (canvas1, text = "* Close the screen with @", font = ('Helvetica', '13'), fg ='gray8')
titleLabel_j.place (relx = 0.85, rely = 0.01)
canvas1.place (x = 0, y = 0) # canvas
label_a = tkinter.Label (canvas1, text = "Hello. This front room inventory management system.", font = ( 'BIZ UD Gothic', '25', 'bold'), bg = "misty rose", fg = ' firebrick4')
label_a.place (relx = 0.29, rely = 0.25)
label_b = tkinter.Label (canvas1, text = "Place the cursor in the box below and hold the employee QR code on the attached sheet to log in.", Font = ('BIZ UD Gothic', '15'), fg ='gray4') #text
label_b.place (relx = 0.23, rely = 0.35)
label1 = tkinter.Label (canvas1, text = "↓ Name Here ↓", font = ('BIZ UD Gothic', '23'), fg ='gray4')
label1.place (relx = 0.42, rely = 0.42)
button = tkinter.Button (canvas1, text = "Go", font = ('BIZ UD Gothic', '15'), command = lambda: transition_button1 (canvas1), fg ='gray4')
button.place (relx = 0.45, rely = 0.53)
button_kaisetsu = tkinter.Button (canvas1, text = "Details of usage", font = ('BIZ UD Gothic', '15'), command = lambda: kaisetsuPage (), fg ='gray4')
button_kaisetsu.place (relx = 0.85, rely = 0.05)
entry1 = tkinter.Entry (canvas1, font = ('Helvetica', '20'))
entry1.place (relx = 0.39, rely = 0.47)
def kaisetsuPage ():
    window = tkinter.Tk ()
    window.geometry ("500x600")
def transition_button1 (widget):
    widget.place_forget ()
    canvas2 = tkinter.Canvas (bd = 0, width = 1366, height = 768)
    canvas2.place (x = 0, y = 0)
    Generate and place Scrollbar on #Top Widget
    bar = tk.Scrollbar (window, orient = tk.VERTICAL)bar.pack (side = tk.RIGHT, fill = tk.Y)
    bar.config (command = canvas2.yview)
    Place #Canvas Widget on TopWidget
    canvas2.config (yscrollcommand = bar.set)
    canvas2.config (scrollregion = (0, 0, 500, 900))
    canvas2.pack (side = tk.LEFT, fill = tk.BOTH, expand = True)
    Generate #Frame Widget
    frame = tk.Frame (canvas2)
    Place #Frame Widget on Canvas Widget ()
    canvas2.create_window ((0, 0), window = frame, anchor = tk.NW, width = 1366, height = 768)
    Display various labels on #Frame Widget
    aaa = tk.Label (frame, text ='magic', font = ('Helvetica', '15'))
    aaa.place (x = 63, y = 80)
    bbb = tk.Label (frame, text ='bbb', font = ('Helvetica', '15'))
    bbb.place (x = 63, y = 130)
    ccc = tk.Label (frame, text ='ccc', font = ('Helvetica', '15'))
    ccc.place (x = 63, y = 180)
    ddd = tk.Label (frame, text ='ddd', font = ('Helvetica', '15'))
    ddd.place (x = 63, y = 230)
    eee = tk.Label (frame, text ='eee', font = ('Helvetica', '15'))
    eee.place (x = 63, y = 280)
    fff = tk.Label (frame, text ='fff', font = ('Helvetica', '15'))
    fff.place (x = 63, y = 330)
    t0 = Text (width = 30, height = 40)
    t0.place (relx = 0.8, rely = 0.2)
    titleLabel5 = tk.Label (frame, text = data1, font = ('Helvetica', '15'))
    titleLabel5.place (x = 300, y = 78)
    titleLabel6 = tk.Label (frame, text = data2, font = ('Helvetica', '15'))
    titleLabel6.place (x = 300, y = 128)
    titleLabel7 = tk.Label (frame, text = data3, font = ('Helvetica', '15'))
    titleLabel7.place (x = 300, y = 178)
    sptxt1 = StringVar ()
    sp1 = Spinbox (frame, textvariable = sptxt1, from_ = 0, to = 10, increment = 1)
    sp1.place (x = 485, y = 80)
    sptxt2 = StringVar ()
    sp2 = Spinbox (frame, textvariable = sptxt2, from_ = 0, to = 10, increment = 1)
    sp2.place (x = 485, y = 130)
    sptxt3 = StringVar ()
    sp3 = Spinbox (frame, textvariable = sptxt3, from_ = 0, to = 10, increment = 1)
    sp3.place (x = 485, y = 180)
    date = datetime.datetime.now ()
    print (date.year, "year", date.month, "month", date.day, "day", date.hour, "hour", date.minute, "minute", date.second, "second")
    print (entry1.get (), "Mr. Hello!")
    file = "Goods issue history.txt"
    f4 = open (file, "a", encoding = "utf_8")
    print (date.year, "year", date.month, "month", date.day, "day", date.hour, "hour", date.minute, "minute", date.second, "second",
          file = f4)
    print (entry1.get (), "san has entered the room.", File = f4)
    titleLabel_1 = tk.Label (canvas2, text = "Please specify the number of equipment I want to issue and press the" Issue "button.", Font = ('BIZ UD Gothic', '15'), bg = "misty rose" , fg ='gray4')
    titleLabel_1.place (relx = 0, rely = 0)
    titleLabel_2 = tk.Label (canvas2, text = "equipment name, number of equipment required", font = ('BIZ UD Gothic', '25','bold'), bg = "misty rose", fg ='firebrick4')
    titleLabel_2.place (relx = 0, rely = 0.035)
    back_button = tkinter.Button (canvas2, text = "back", command = lambda: transition_button2 (canvas1), font = ('BIZ UD Gothic', '13')) # Transition Button
    back_button.place (relx = 0.9, rely = 0.01)
    kakutei_button = tk.Button (canvas2, text = "confirmed", font = ('BIZ UD Gothic', '13'),
                               command = lambda: Kakutei (canvas2))kakutei_button.place (relx = 0.4, rely = 0.85)
    def Kakutei (widget):
        f1 = open ('magic red.txt')
        data1 = f1.read ()
        f1.close ()
        o = int (data1)
        p = int (sp1.get ())
        a = o --p
        titleLabel5.destroy ()
        f1 = open ('magic red.txt','w')
        print (a, file = f1)
        titleLabel10 = tk.Label (frame, text = a, font = ('Helvetica', '15'))
        titleLabel10.place (x = 300, y = 78)
        f2 = open ('Magic Blue.txt')
        data2 = f2.read ()
        f2.close ()
        q = int (data2)
        r = int (sp2.get ())
        b = q --r
        titleLabel6.destroy ()
        f2 = open ('Magic Blue.txt','w')
        print (b, file = f2)
        titleLabel8 = tk.Label (frame, text = b, font = ('Helvetica', '15'))
        titleLabel8.place (x = 300, y = 128)
        f3 = open ('magic black.txt')
        data3 = f3.read ()
        f3.close ()
        s = int (data3)
        t = int (sp3.get ())
        c = s --t
        titleLabel7.destroy ()
        f3 = open ('Magic Black.txt','w')
        print (c, file = f3)
        titleLabel9 = tk.Label (frame, text = c, font = ('Helvetica', '15'))
        titleLabel9.place (x = 300, y = 178)
        print ("remaining inventory")
        print ("magic red", a, "pieces", "(-", p, "pieces)")
        print ("Magic Blue", b, "pieces", "(-", r, "pieces)")
        print ("magic black", c, "pieces", "(-", t, "pieces)")
        file = "Goods issue history.txt"
        f4 = open (file, "a", encoding = "utf_8")
        print ("magic red", a, "pieces", "(-", p, "pieces)", file = f4)
        print ("magic blue", b, "pieces", "(-", r, "pieces)", file = f4)
        print ("magic black", c, "pieces", "(-", t, "pieces)", file = f4)
        t0.insert (tk.END, "Magic Red")
        t0.insert (tk.END, a)
        t0.insert (tk.END, "pieces")
        t0.insert (tk.END, "(-")
        t0.insert (tk.END, p)
        t0.insert (tk.END, "pieces) \ n")
        t0.insert (tk.END, "Magic Blue")
        t0.insert (tk.END, b)
        t0.insert (tk.END, "pieces")
        t0.insert (tk.END, "(-")
        t0.insert (tk.END, r)
        t0.insert (tk.END, "pieces) \ n")t0.insert (tk.END, "Magic Black")
        t0.insert (tk.END, c)
        t0.insert (tk.END, "pieces")
        t0.insert (tk.END, "(-")
        t0.insert (tk.END, t)
        t0.insert (tk.END, "pieces) \ n")
        # back_button execution result
    def transition_button2 (widget):
        canvas2.pack_forget ()
        back_button.pack_forget ()
        bar.pack_forget ()
        frame.pack_forget ()
        aaa.pack_forget ()
        bbb.pack_forget ()
        ccc.pack_forget ()
        ddd.pack_forget ()
        eee.pack_forget ()
        fff.pack_forget ()
        titleLabel_1.pack_forget ()
        titleLabel_2.pack_forget ()
        print ("Thanks for your hard work!")
        file = "Goods issue history.txt"
        f4 = open (file, "a", encoding = "utf_8")
        print (entry1.get (), "san has left the room.", File = f4)
        #Restore screen 1
        canvas1 = tkinter.Canvas (bd = 0, width = 1366, height = 768)
        titleLabel_j = tk.Label (canvas1, text = "* close screen with @", font = ('Helvetica', '13'), fg ='gray8')
        titleLabel_j.place (relx = 0.85, rely = 0.01)
        canvas1.place (x = 0, y = 0) # canvas
        label_a = tkinter.Label (canvas1, text = "Hello. This front room inventory management system.", font = ( 'BIZ UD Gothic', '25', 'bold'), bg = "misty rose", fg = ' firebrick4')
        label_a.place (relx = 0.29, rely = 0.25)
        label_b = tkinter.Label (canvas1, text = "Place the cursor in the box below and hold the employee QR code on the attached sheet to log in.", Font = ('BIZ UD Gothic', '15'), fg ='gray4') #text
        label_b.place (relx = 0.23, rely = 0.35)
        label1 = tkinter.Label (canvas1, text = "↓ Name Here ↓", font = ('BIZ UD Gothic', '23'), fg ='gray4')
        label1.place (relx = 0.42, rely = 0.42)
        button = tkinter.Button (canvas1, text = "Go", font = ('BIZ UD Gothic', '15'), command = lambda: transition_button1 (canvas1), fg ='gray4')
        button.place (relx = 0.45, rely = 0.53)
        entry2 = tkinter.Entry (canvas1, font = ('Helvetica', '20'))
        entry2.place (relx = 0.39, rely = 0.47)
        button_kaisetsu = tkinter.Button (canvas1, text = "Details of usage", font = ('BIZ UD Gothic', '15'), command = lambda: kaisetsuPage (), fg ='gray4')
        button_kaisetsu.place (relx = 0.85, rely = 0.05)
if __name__ == "__main__":
    def process_b ():
        quit ()
    def key (event):
        if event.char == "@":
            process_b ()
    window.bind ("<Key>", key)
    window.focus_set ()
window.mainloop ()
What I tried

I changed the name of entry1.get () above, but I got an error.
We apologize for the inconvenience, but thanks for your cooperation.

  • Answer # 1

    Tentative Measure 1: Make a global declaration

      def transition_button2 (widget):
            global entry1
            ...
            print (entry1.get (), "san has left the room.", File = f4)
            #Restore screen 1
            ...
            entry1 = tkinter.Entry (canvas1, font = ('Helvetica', '20'))
            entry1.place (relx = 0.39, rely = 0.47)

    * The code cannot be executed. unconfirmed

    Computational Countermeasure 2: Use StringVar
    nameVar = tkinter.StringVar ()
    entry1 = tkinter.Entry (canvas1, textvar = nameVar, font = ('Helvetica', '20'))
    ...
        def transition_button2 (widget):
            ...
            print (nameVar.get (), "Mr. Hello!")
            #Restore screen 1
            Erase the contents of #StringVar. Reuse objects without destroying them
            nameVar.set ("")
            entry1 = tkinter.Entry (canvas1, textvar = nameVar, font = ('Helvetica', '20'))
            entry1.place (relx = 0.39, rely = 0.47)

    * The code cannot be executed. unconfirmed


    Fundamental measures (significant changes required)

    Screen 1 can be restored with the same code at the time of initialization and after the second time.
    It is better to cut it out into another function.

    In the current code, there are some widgets that are just hidden and remain
    There is a problem that unopened widgets accumulate when called many times.
    Unless you call it thousands of times, it won't be a problem,
    It is also a factor that complicates the program, so it can be improved.

    For more fundamental measures,
    Build the screen on the Frame, stack it like a layer in advance,
    It is recommended to implement the screen transition, which switches the display by bringing it up with the tkraise method.
    It also makes it easier to manage the code for each screen and solves the above problems.

  • Answer # 2

    I haven't looked at it in detail, but ...

    entry1 = tkinter.Entry (canvas1, font = ('Helvetica', '20'))

    entry1 seems to be created in relation to canvas1

    #Restore screen 1
    canvas1 = tkinter.Canvas (bd = 0, width = 1366, height = 768)

    Since canvas1 seems to be creating a new one here,
    If you set entry1 again, it will make sense ...