Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to make a game with Python, but I ask a question because it is difficult to make. The question is, when I press the button join, I check if the value of entry is duplicated and if not, I save it in a file called db.txt. I get an error. What should I do?

the fruit of my efforts

import tkinter
root = tkinter.Tk()
root.title("game")
root.geometry("1000x700")
SPSP = 100
index = 0
btn_c = 0
mouse_x = 0
mouse_y = 0

bg2 = tkinter.PhotoImage(file = "C:\Users\user\Pictures\Defense                                                        game\Map.png")
bg = tkinter.PhotoImage(file = "C:\Users\user\Pictures\Defense game\game1.png")
bg3 = tkinter.PhotoImage(file = "C:\Users\user\Pictures\Defense game\Login screen.png")
def click():
    global bg, bg2, index, btn_c
    canvas2 = tkinter.Canvas(root, width = 1000, height = 600)
    canvas2.pack()
    index = 1
    if index == 1:
        bg2 = tkinter.PhotoImage(file = "C:\Users\user\Pictures\Defense game\Map.png")
        canvas2.create_image(500, 300, image = bg2)
        canvas2.place(x = 0, y = 0)


def login():
    global bg, bg2, bg3, index, btn_c
    canvas3 = tkinter.Canvas(root, width = 1000, height = 600)
    canvas3.pack()
    index = 2
    if index == 2:
        bg3 = tkinter.PhotoImage(file = "C:\Users\user\Pictures\Defense game\Login screen.png")
        canvas3.create_image(500, 300, image = bg3)
        canvas3.place(x = 0, y = 0)
        label4 = tkinter.Label(text = "ID", font = ("???", 30))
        label4.place(x = 10, y = 200)
        label4 = tkinter.Label(text = "pw", font = ("???", 30))
        label4.place(x = 10, y = 300)
        entry = tkinter.Entry(root, width = 20)
        entry.insert(0, "More than 3 characters and less than 20 characters")

        def db_join():
            save = 0
            global data_ID, data_pw
            if len(entry.get()) > 3 and len(entry.get()) <20 and len(entry2.get()) > 5 and len(entry2.get()) <20:
            try:
                    with open("db.txt") as user_db:
                        entry_text = entry.get()
                        entry2_text  = entry2.get()
                        for line in user_db:
                            line = line.strip().split()
                            if line[0] == entry_text:
                                save = 1
                                break
                except FileNotFoundError:
                    pass

                if save == 0:
                    f = open("db.txt", "a")
                    data_ID = entry.get()
                    data_pw = entry2.get()
                    f.write(data_ID + " " + data_pw +  "
")
                    f.close()







        def clear(event):
            if entry.get() == "More than 3 characters and less than 20     characters":
                entry.delete(0, len(entry.get()))

        def clear2(event):
            if entry2.get() == "More than 5 characters and less than 20 characters":
                entry2.delete(0, len(entry2.get()))

        entry.bind("<Button-1>", clear)
        entry.place(x = 40, y = 250)
        entry2 = tkinter.Entry(root, width = 20)
        entry2.config(show = "*")
        entry2.insert(0, "More than 5 characters and less than 20 characters")
        entry2.bind("<Button-1>", clear2)
        entry2.place(x = 40, y = 350)



        btn_Join = tkinter.Button(text = "join membership", font = ("???", 50), fg = "skyblue", command = db_join)
        btn_Join.place(x = 300, y = 400)
        btn_login = tkinter.Button(text = "Login", font = ("???", 50), fg = "skyblue")
        btn_login.place(x = 300, y = 250)


def game_main():
    global bg, bg2, index, btn_c
    canvas = tkinter.Canvas(root, width = 1000, height = 600)
    canvas.pack()
    if index == 0:
        bg = tkinter.PhotoImage(file = "C:\Users\user\Pictures\Defense game\game1.png")
        canvas.create_image(500, 300, image = bg)
        btn = tkinter.Button(text = "start", font = ("???",20), command = click)
        btn.place(x = 460, y = 600)
        btn2 = tkinter.Button(text = "join membership", font = ("???", 20), command = login)
        btn2.place(x = 550, y = 600)


    




my_base = [
    [0,0],
    [0,0],
    [0,0],
    [0,0],
    [0,0],
    [0,0], 
]
your_base = [
    [1,1],
    [1,1], 
    [1,1], 
    [1,1], 
    [1,1], 
    [1,1],
]


game_main()
root.mainloop()

These are my codes. What I want is to check if the entry value is duplicated here, and if it is not duplicated, put it in the txt file and cancel it if it is duplicated


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
308 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...