如何访问文件2中的文件1

时间:2019-12-10 06:51:54

标签: python-3.x tkinter

fil_1:

from tkinter import *
from tkinter import messagebox
from PIL import ImageTk

import mysql.connector as mysql
import tkinter
import storge

con =storge.connect()

class LoginFrame: 

    def __init__(self):

        self.root=tkinter.Tk()
        self.root.title("Login")

        self.root.geometry("900x450+0+0")
        self.icon = tkinter.PhotoImage(file = "project/logo_1.png")

        self.label = tkinter.Label(self.root, image = self.icon)
        self.label.pack()

        self.pass_w = tkinter.PhotoImage(file = "project/pas_1.png")
        self.pass_w_label = tkinter.Label(self.root, image = self.pass_w)
        self.pass_w_label.pack()

        self.username_1 = tkinter.PhotoImage(file = "project/username.png")
        self.username_1_label = tkinter.Label(self.root, image = self.username_1)
        self.username_1_label.pack()

        self.adn_3_1 = tkinter.PhotoImage(file = "project/adn_3.png")
        self.adn_3_1_label = tkinter.Label(self.root, image = self.adn_3_1)
        self.adn_3_1_label.pack()

        self.title=Label(self.root,text="Admin",font=("times new roman",40,"bold"))
        self.title.place(x=0,y=0,relwidth=1)

        self.Login_Frame=Frame(self.root,bd=5)
        self.Login_Frame.place(x=450,y=65,height=450)

        self.logo_lbl=Label(self.Login_Frame,image=self.adn_3_1,bd=0)
        self.logo_lbl.grid(row=0,columnspan=2,pady=20)

        self.lbluser=Label(self.Login_Frame,text="Username",image=self.username_1,compound=LEFT,font= 
        ("times new roman",20,"bold"))

        self.lbluser.grid(row=1,column=0,padx=20,pady=10)
        self.txtuser=Entry(self.Login_Frame,bd=5,relief=GROOVE,font=("",15))
        self.txtuser.grid(row=1,column=1,padx=20)

        self.lblpassword=Label(self.Login_Frame,text="Password",image=self.pass_w ,compound=LEFT,font= 
        ("times new roman",20,"bold"))

        self.lblpassword.grid(row=2,column=0,padx=20,pady=10)
        self.txtpassword=Entry(self.Login_Frame,bd=5,relief=GROOVE,font=("",15))
        self.txtpassword.grid(row=2,column=1,padx=20)



        self.btn_login=Button(self.Login_Frame,text="Login",command=self.insrt,width=15,font=("times new 
        roman",14,"bold"),bg="black",fg="white")
        self.btn_login.grid(row=3,column=1,pady=10)

        self.root.mainloop()

    def insrt(self):
        username=self.txtuser.get().strip()
        password=self.txtpassword.get().strip()

        if (username=="" or password==""):
            messagebox.showerror("Error","All field are rquiarde")
        else:
            cursor =con.cursor()
            cursor.execute("insert into login value('"+username+"','"+password+"')")
            cursor.execute("commit");

            self.txtuser.delete(0,'end')
            self.txtpassword.delete(0,'end')
            messagebox.showinfo("info","inserted successfully")
            con.close();


       if __name__ == "__main__":
           LoginFrame()

fil2:

from tkinter import *
import tkinter as tk
from tkinter.ttk import Combobox
import mysql.connector as mysql
from PIL import Image, ImageTk
from login_1 import loginFrame
import tkinter
import storge

con =storge.connect()

def create_header(master):

    Header_Frame=tk.Frame(master,bg="#088A85")
    Header_Frame.pack(side=TOP, fill=X)
    prinfo=Button(Header_Frame,text="Profile info",bg='#0B615E',foreground="white",font=("time new 
    roman",10,"bold"))

    prinfo.grid(row=0, column=0,padx=2,pady=(25,0))

    pas=Button(Header_Frame,text="Change Password",bg='#0B615E',foreground="white",font=("time new 
    roman",10,"bold",'underline'))
    pas.grid(row=0, column=1,padx=2, pady=(25,0))

    login=Button(Header_Frame,text="LOG in",bg='#0B615E',foreground="white",font=("time new 
    roman",10,"bold"))
    login.grid(row=0, column=2,padx=2, pady=(25,0))

    return Header_Frame

   if __name__ == "__main__":

      root=tkinter.Tk()
      root.title("header")

      root.geometry("900x450")
      ff = create_header(root)

      aa=loginframe(root)
      root.mainloop()

1 个答案:

答案 0 :(得分:0)

正如您所澄清的,这是module 1 objectsmodule 2的导入,那么我建议在您的项目目录中使用此结构。

--your_project_directory
|
|---file1.py
|---file2.py

在file2.py中添加from file1 import LoginFrame,然后您就可以在file2代码中使用LoginFrame类。

您可以通过从your_project_directory运行python进行测试,然后发出以下导入

$ python

>>>from file1 import LoginFrame
>>>print(LoginFrame) # should not throw an exception