Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

python - Why is my Register page appearing when I run login page?

When I run my page for login it displays my register page. When I close my register page it appears. Anybody knows whats the issue?

Here is my login page

from tkinter import*
from tkinter import ttk
from tkinter import messagebox
import PIL
from PIL import ImageTk
from PIL import Image

import register
import pymysql




class Login:


def __init__(self,root):
    self.root=root
    self.root.title("Scheduling Management System")
    self.root.geometry("1350x768+0+0")
    self.root.resizable(False,False)

    self.txt_user = StringVar()
    self.txt_pass = StringVar()
    self.bg = ImageTk.PhotoImage(file = "Images/bgimage.jpg")
    bg = Label(self.root,image=self.bg).place(x = 0, y= 0, relwidth = 1, relheight = 1)
    framelogin = Frame(self.root,bg="white")
    framelogin.place(x=450,y=100,height=500,width=700)

    title=Label(framelogin,text="Login Here",font=("Arial",30,"bold"),fg="orange",bg="white").place(x=90,y=30)
    nexttitle=Label(framelogin,text="Scheduling Staff System",font=("Times New Roman",18,"bold"),fg="orange",bg="white").place(x=90,y=100)

    userlabel=Label(framelogin,text="Username",font=("Arial",15,"bold"),fg="gray",bg="white").place(x=90,y=140)
    self.txt_user=Entry(framelogin,textvariable = self.txt_user,font=("times new roman",15),bg="lightgray")
    self.txt_user.place(x=90,y=170,width=350,height=35)

    passlabel=Label(framelogin,text="Password",font=("Arial",15,"bold"),fg="gray",bg="white").place(x=90,y=210)
    self.txt_pass=Entry(framelogin,textvariable = self.txt_pass,font=("times new roman",15),show="*",bg="lightgray")
    self.txt_pass.place(x=90,y=240,width=350,height=35)

    forget=Button(framelogin,text="Forgot Password",bg="white",fg="orange",font=("trebuchet ms",12)).place(x=90,y=305)
    reglabel=Label(framelogin,text="Don't Have an Account?",font=("trebuchet ms",12,"bold"),fg="orange",bg="white").place(x=320,y=310)
    registerbutton=Button(framelogin,text="Sign Up",command=register,bg="white",fg="orange",font=("trebuchet ms",12)).place(x=510,y=305)

    loginbutton=Button(framelogin,text="Login",command=self.login,fg="white",bg="orange",font=("sans serif ms",20)).place(x=90,y=350,width="100",height="40")

def login(self):
    if self.txt_user.get() == "" or self.txt_pass.get() == "":
        messagebox.showerror("Error", "Please fill up all fields!")


root = Tk()
obj = Login(root)
root.mainloop()

Here is my register page

from tkinter import *
from tkinter import ttk, messagebox

import PIL
import pymysql
from PIL import ImageTk
from PIL import Image

class Register:
    def __init__(self, root):
        self.root = root
        self.root.title("Registration Page")
        self.root.geometry("1350x768+0+0")
        self.root.config(bg="light blue")

        self.bg = ImageTk.PhotoImage(file="Images/bgimage.jpg")
        bg = Label(self.root,image=self.bg).place(x = 0, y= 0, relwidth = 1, relheight = 1)
        frame1=Frame(self.root,bg="white")
        frame1.place(x=450,y=100,width=700,height=600)

        title=Label(frame1,text="Please enter your information here",font=("trebuchet ms",20,),bg="white",fg="black").place(x=50, y=30)


        fname=Label(frame1,text="First Name",font=("times new roman",15,"bold"),bg="white",fg="black").place(x=50, y=100)
        self.text_fname=Entry(frame1,font=("arial",15,),bg="lightgray")
        self.text_fname.place(x=50, y=130, width=250)
        lname=Label(frame1,text="Last Name",font=("times new roman",15,"bold"),bg="white",fg="black").place(x=370, y=100)
        self.text_lname=Entry(frame1,font=("arial",15,),bg="lightgray")
        self.text_lname.place(x=370, y=130, width=250)
        contact=Label(frame1,text="Contact Number",font=("times new roman",15,"bold"),bg="white",fg="black").place(x=50, y=170)
        self.text_contact=Entry(frame1,font=("arial",15,),bg="lightgray")
        self.text_contact.place(x=50, y=200, width=250)
        email=Label(frame1,text="Email Address",font=("times new roman",15,"bold"),bg="white",fg="black").place(x=370, y=170)
        self.text_email=Entry(frame1,font=("arial",15,),bg="lightgray")
        self.text_email.place(x=370, y=200, width=250)
        question=Label(frame1,text="Security Question",font=("times new roman",15,"bold"),bg="white",fg="black").place(x=50, y=240)
        self.cmbquestion=ttk.Combobox(frame1,font=("times new roman",13),state='readonly',justify=CENTER)
        self.cmbquestion['values']=("Select","Your First Car","Your Mothers First Name", "Your Best Friend Name")
        self.cmbquestion.place(x=50, y=270, width=250)
        self.cmbquestion.current(0)
        answer=Label(frame1,text="Answer",font=("times new roman",15,"bold"),bg="white",fg="black").place(x=370, y=240)
        self.text_answer=Entry(frame1,font=("arial",15,),bg="lightgray")
        self.text_answer.place(x=370, y=270, width=250)
        pwd=Label(frame1,text="Password",font=("times new roman",15,"bold"),bg="white",fg="black").place(x=50, y=310)
        self.text_pwd=Entry(frame1,font=("arial",15,),show="*",bg="lightgray")
        self.text_pwd.place(x=50, y=340, width=250)
        cfmpwd=Label(frame1,text="Confirm Password",font=("times new roman",15,"bold"),bg="white",fg="black").place(x=370, y=310)
        self.text_cfmpwd=Entry(frame1,font=("arial",15,),show="*",bg="lightgray")
        self.text_cfmpwd.place(x=370, y=340, width=250)

        self.btn= ImageTk.PhotoImage(file="images/register.png")
        btn = Button(frame1,image=self.btn, bd=0, command = self.registerdata,cursor = "hand2").place(x=50, y = 420)



    def registerdata(self):
        if self.text_fname.get()=="" or self.text_lname.get()=="" or self.text_contact.get()=="" or self.text_email.get()=="" or self.cmbquestion.get()=="Select" or self.text_pwd.get()=="" or self.text_cfmpwd.get()=="":
            messagebox.showerror("Error","All fields are required!",parent=self.root)
        elif self.text_pwd.get()!=self.text_cfmpwd.get():
            messagebox.showerror("Error","Passwords must be the same!",parent=self.root)
        else:
            try:
                con=pymysql.connect(host="localhost",user="root",password="",database="employee")
                cur=con.cursor()
                cur.execute("select * from employeelist where email=%s", self.text_email.get())
                row=cur.fetchone()
                print(row)
                if row!=None:
                    messagebox.showerror("Error","User Already Exists. Please Register With a New Email",parent=self.root)
                else:
                    cur.execute("insert into employeelist (fname,lname,contact,email,question,answer,password) values(%s,%s,%s,%s,%s,%s,%s)",
                                (self.text_fname.get(),self.text_lname.get(),self.text_contact.get(),self.text_email.get(),self.cmbquestion.get(),self.text_answer.get(),self.text_pwd.get()))
                    con.commit() #do changes to database
                    con.close()
                    messagebox.showinfo("Success","Registration Successful",parent=self.root)
            except Exception as ex:
                messagebox.showerror("Error",f"Error due to: {str(ex)}",parent=self.root)
root = Tk()
obj = Register(root)
root.mainloop()

I do not know why when I run my login page it appears to my registration page but when I close my registration page it will show my login page. I have defined all variables accordingly but still am facing this error.

question from:https://stackoverflow.com/questions/65919164/why-is-my-register-page-appearing-when-i-run-login-page

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

1 Reply

0 votes
by (71.8m points)

When you import register in the login file, it runs the code at the bottom of the file, so it is run first and the register window displays. Then once you close the register window, the code at the bottom of login is run and the login window is displayed. To avoid this, change the bottom of login to something like this:

if __name__ == "__main__":
    root = Tk()
    obj = Login(root)
    root.mainloop()

This will only run the code at the bottom if the file is run directly, not if it is imported, which should resolve your issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...