본문 바로가기
PROGRAMING📚/Python📑

thinter-데이터 불러오기

Ta이니 2024. 11. 12.
728x90
반응형

analysisVisualization/database/mainStart.py 여기서 시작함

from analysisVisualization.database.controller.MemberController import MemberController

controller = MemberController()
controller.member_controller('Login')

DaoMember

from analysisVisualization.database.dao.DaoSet import DaoSet as dao

'''
CREATE TABLE member7(
    memno INT PRIMARY KEY AUTO_INCREMENT,
    NAME VARCHAR(20) NOT NULL,
    id VARCHAR(20) NOT NULL,
    pw VARCHAR(20) NOT NULL);
'''

class MemberDao(dao):
  # 생성자 생성
  def __init__(self):
    self.con = dao.connect(self)
    self.cursor = self.con.cursor()

  def __del__(self):
    try:
      self.disconnect()
    except Exception as e:
      print(e)

  def get_all(self):
    cur = self.con.cursor()
    sql = "select * from member7 "
    cur.execute(sql)
    return cur.fetchall()
    # rs = self.cursor.execute("select * from member7 ")
    # return rs.fetchall()

  def login_check(self, mem):
    rs = self.cursor.execute(f"select * from member7 "
                             f"where id='{mem.id}' and pw='{mem.pw}'")
    return rs

  def insert_one(self, mem):
    self.cursor.execute(f"insert into member7 (id, name, pw) "
                        f"values('{mem.id}','{mem.name}','{mem.pw}') ")
    self.con.commit()
    return self.cursor.rowcount

  def modify_one(self, mem):
    self.cursor.execute(f"update member7 set name='' , pw='' "
                        f"where id='{mem.id}' ")
    self.con.commit()
    return self.cursor.rowcount

  def delete_one(self, mem):
    self.cursor.execute(f"delete from member7 where id='{mem.id}' ")
    self.con.commit()
    return self.cursor.rowcount

MemberController

  def member_controller(self, cmd=None, *str):
    print("cmd:", cmd, "/", "MainController id:", id(self))
    if cmd == 'Login':
      from analysisVisualization.database.ui.login_win import LoginWindow
      LoginWindow()
    elif cmd == 'List':
      from analysisVisualization.database.ui.list_win import ListWindow
      ListWindow()
    elif cmd == 'Join':
      from analysisVisualization.database.ui.join_win import JoinWindow
      JoinWindow()
    else:
      messagebox.showwarning("Notice", "없는 명령입니다.")
from tkinter import messagebox

class MemberController(object):
  def __new__(cls, *args, **kwargs):
    if not hasattr(cls, 'instance'):
      cls.instance = super(MemberController, cls).__new__(cls)
      print("Singleton 주소: ", cls.instance)
    return cls.instance

  def member_controller(self, cmd=None, *str):
    print("cmd:", cmd, "/", "MainController id:", id(self))
    if cmd == 'Login':
      from analysisVisualization.database.ui.login_win import LoginWindow
      LoginWindow()
    elif cmd == 'List':
      from analysisVisualization.database.ui.list_win import ListWindow
      ListWindow()
    elif cmd == 'Join':
      from analysisVisualization.database.ui.join_win import JoinWindow
      JoinWindow()
    else:
      messagebox.showwarning("Notice", "없는 명령입니다.")

login_win

login_win 전체 코드

더보기
import tkinter.font
from tkinter import *
from tkinter import messagebox
from analysisVisualization.database.dao.DaoMember import MemberDao
from analysisVisualization.database.ui.util import center_win
from analysisVisualization.database.vo.member_vo import Member


class LoginWindow:
  def __init__(self):
    from analysisVisualization.database.controller.MemberController import MemberController
    self.controller = MemberController()
    self.dao = MemberDao()
    self.window = Tk()

    font = tkinter.font.Font(family="맑은 고딕", size=12)
    # place() 절대 위치에 컴포넌트를 배치 할 수 있다.
    lbId = tkinter.Label(self.window, text="ID", font=font)
    lbPass = tkinter.Label(self.window, text="Pass", font=font)
    self.entryId = tkinter.Entry(self.window, relief="solid", borderwidth=1, font=font)
    self.entryPass = tkinter.Entry(self.window, relief="solid", borderwidth=1, font=font, show="*")
     
    # 위치
    lbId.place(x=10, y=8)
    self.entryId.place(x=50, y=10)
    lbPass.place(x=10, y=38)
    self.entryPass.place(x=50, y=40)
    # 버튼
    btnLogin = Button(self.window, text="Login", width="10",
                      overrelief="groove", anchor="n", command=self.check_login)
    btnLogin.place(x=30, y=80)

    btnJoin = Button(self.window, text="Join", width="10",
                     overrelief="groove", anchor="n", command=self.go_join)
    btnJoin.place(x=130, y=80)
    
    self.entryId.focus()
    center_win(self.window, 250, 120)
    self.window.mainloop()

  def check_login(self):
    if str(self.entryId.get()) == "":
      messagebox.showinfo("알림", "ID를 확인하여 주세요")
      self.entryId.focus()
      return

    if str(self.entryPass.get()) == "":
      messagebox.showinfo("알림", "Password를 확인하여 주세요")
      self.entryPass.focus()
      return

    mem = Member()
    mem.id = self.entryId.get()
    mem.pw = self.entryPass.get()
    result = self.dao.login_check(mem)
    print(">>", result)
    if result == 0:
      # 입력되어있는 값을 다 지워줌
      self.entryId.delete(0, len(self.entryId.get()))
      self.entryPass.delete(0, len(self.entryPass.get()))
      messagebox.showwarning("Notice", "없는 계정입니다.")
      self.entryId.focus()
    else:
      self.window.destroy()
      self.controller.member_controller('List')

  def go_join(self):
    self.window.destroy()
    self.controller.member_controller('Join')

def __init__(self):
    from analysisVisualization.database.controller.MemberController import MemberController
    self.controller = MemberController()
    self.dao = MemberDao()
    self.window = Tk()

    font = tkinter.font.Font(family="맑은 고딕", size=12)
    # place() 절대 위치에 컴포넌트를 배치 할 수 있다.
    lbId = tkinter.Label(self.window, text="ID", font=font)
    lbPass = tkinter.Label(self.window, text="Pass", font=font)
    self.entryId = tkinter.Entry(self.window, relief="solid", borderwidth=1, font=font)
    self.entryPass = tkinter.Entry(self.window, relief="solid", borderwidth=1, font=font, show="*")

    # 위치
    lbId.place(x=10, y=8)
    self.entryId.place(x=50, y=10)
    lbPass.place(x=10, y=38)
    self.entryPass.place(x=50, y=40)
    # 버튼
    btnLogin = Button(self.window, text="Login", width="10",
                      overrelief="groove", anchor="n", command=self.check_login)
    btnLogin.place(x=30, y=80)

    btnJoin = Button(self.window, text="Join", width="10",
                     overrelief="groove", anchor="n", command=self.go_join)
    btnJoin.place(x=130, y=80)

    self.entryId.focus()
    center_win(self.window, 250, 120)
    self.window.mainloop()

init 안에 초기화를 해줌, 위의 코드에서는 id, pass, login 버튼, join 버튼을 구현 해줌

 

  def check_login(self):
    if str(self.entryId.get()) == "":
      messagebox.showinfo("알림", "ID를 확인하여 주세요")
      self.entryId.focus()
      return

    if str(self.entryPass.get()) == "":
      messagebox.showinfo("알림", "Password를 확인하여 주세요")
      self.entryPass.focus()
      return

    mem = Member()
    mem.id = self.entryId.get()
    mem.pw = self.entryPass.get()
    result = self.dao.login_check(mem)
    print(">>", result)
    if result == 0:
      # 입력되어있는 값을 다 지워줌
      self.entryId.delete(0, len(self.entryId.get()))
      self.entryPass.delete(0, len(self.entryPass.get()))
      messagebox.showwarning("Notice", "없는 계정입니다.")
      self.entryId.focus()
    else:
      self.window.destroy()
      self.controller.member_controller('List')

  def go_join(self):
    self.window.destroy()
    self.controller.member_controller('Join')

join_win

join_win 전체 코드

더보기
from tkinter import *
from tkinter import messagebox
from analysisVisualization.database.dao.DaoMember import MemberDao
from analysisVisualization.database.ui.util import center_win
from analysisVisualization.database.vo.member_vo import Member

class JoinWindow:
  def __init__(self):
    from analysisVisualization.database.controller.MemberController import MemberController
    self.controller = MemberController()
    self.join = Tk()
    self.id, self.pw= StringVar(), StringVar()
    self.namew = StringVar()
    self.dao = MemberDao()
    Label(self.join, text="Name : ") \
      .grid(row=0, column=0, padx=10, pady=10)
    Label(self.join, text="ID : ") \
      .grid(row=1, column=0, padx=10, pady=10)
    Label(self.join, text="Password : ") \
      .grid(row=2, column=0, padx=10, pady=10)

    self.ent_name = Entry(self.join, textvariable=self.namew)
    self.ent_name.grid(row=0, column=1, padx=10, pady=10)

    self.ent_id = Entry(self.join, textvariable=self.id)
    self.ent_id.grid(row=1, column=1, padx=10, pady=10)

    self.ent_pw = Entry(self.join, textvariable=self.pw, show="*")
    self.ent_pw.grid(row=2, column=1, padx=10, pady=10)

    Button(self.join, text="Regist", width=10, command=self.regist2) \
      .grid(row=3, column=0, padx=10, pady=10, sticky='e')
    Button(self.join, text="Close", width=10, command=self.go_login) \
      .grid(row=3, column=1, padx=10, pady=10, sticky='')

    # self.ent_id.focus()
    center_win(self.join, 270, 180)
    self.join.title="회원 가입"
    self.ent_name.focus()
    self.join.mainloop()

  def regist2(self):
    if str(self.namew.get()) == "":
      messagebox.showinfo("Notice", "이름을 확인해주세요")
      self.ent_name.focus()
      return

    if str(self.id.get()) == "":
      messagebox.showinfo("Notice", "ID를 확인해주세요")
      self.ent_id.focus()
      return

    if str(self.pw.get()) == "":
      messagebox.showinfo("Notice", "비밀번호를 확인해주세요")
      self.ent_pw.focus()
      return

    mem = Member()
    mem.name = self.namew.get()
    mem.id = self.id.get()
    mem.pw = self.pw.get()
    rowCount = self.dao.insert_one(mem)

    if rowCount>0:
      messagebox.showinfo("Notice", f"{rowCount}건이 추가 되었습니다.")
      self.join.destroy()
      self.controller.member_controller('Login')
    else :
      messagebox.showinfo("Notice", f"등록에 실패 했습니다.")
  def go_login(self):
    self.join.destroy()
    self.controller.member_controller('Login')

 def __init__(self):
    from analysisVisualization.database.controller.MemberController import MemberController
    self.controller = MemberController()
    self.join = Tk()
    self.id, self.pw= StringVar(), StringVar()
    self.namew = StringVar()
    self.dao = MemberDao()
    Label(self.join, text="Name : ") \
      .grid(row=0, column=0, padx=10, pady=10)
    Label(self.join, text="ID : ") \
      .grid(row=1, column=0, padx=10, pady=10)
    Label(self.join, text="Password : ") \
      .grid(row=2, column=0, padx=10, pady=10)

    self.ent_name = Entry(self.join, textvariable=self.namew)
    self.ent_name.grid(row=0, column=1, padx=10, pady=10)

    self.ent_id = Entry(self.join, textvariable=self.id)
    self.ent_id.grid(row=1, column=1, padx=10, pady=10)

    self.ent_pw = Entry(self.join, textvariable=self.pw, show="*")
    self.ent_pw.grid(row=2, column=1, padx=10, pady=10)

    Button(self.join, text="Regist", width=10, command=self.regist2) \
      .grid(row=3, column=0, padx=10, pady=10, sticky='e')
    Button(self.join, text="Close", width=10, command=self.go_login) \
      .grid(row=3, column=1, padx=10, pady=10, sticky='')

    # self.ent_id.focus()
    center_win(self.join, 270, 180)
    self.join.title="회원 가입"
    self.ent_name.focus()
    self.join.mainloop()

list_win

list_win 전체 코드

더보기
import tkinter
import tkinter.font
from tkinter import ttk

from analysisVisualization.database.dao.DaoMember import MemberDao
from analysisVisualization.database.ui.util import center_win


class ListWindow:
  def __init__(self):
    from analysisVisualization.database.controller.MemberController import MemberController
    self.controller = MemberController()
    self.dao = MemberDao()

    self.list = tkinter.Tk()
    self.list.title("Member List")

    font = tkinter.font.Font(family="맑은 고딕", size=30, slant="italic")
    self.lbl = tkinter.Label(self.list, text="Member List", font=font)
    self.lbl.config(bg="white")
    self.lbl.pack(side="top", fill="both", padx=10)


    # treeview 표
    self.trv = tkinter.ttk.Treeview(
      self.list, columns=["MemNo", "이름", "ID"],displaycolumns=["MemNo", "이름", "ID"])
    self.trv.pack(side="bottom", fill="both", padx=10, pady=10)

    self.trv.column("#0", width=100)
    self.trv.heading("#0", text="index", anchor="w")

    self.trv.column("#1", width=100, anchor="center")
    self.trv.heading("MemNo", text="MemNo", anchor="center")

    self.trv.column("#2", width=150, anchor="center")
    self.trv.heading("이름", text="이름", anchor="center")

    self.trv.column("#3", width=150, anchor="center")
    self.trv.heading("ID", text="ID", anchor="center")

    self.trv.bind('<ButtonRelease-1>', self.click_item)

    # db로 부터 들고 온 데이터를  TreeView에 담기
    trv_list = self.dao.get_all()
    if trv_list != None:
      for i in range(len(trv_list)):  # iid =index id
        self.trv.insert('', 'end', text=i, values=trv_list[i], iid=str(i) + ")")

    center_win(self.list, 540, 300)
    self.list.mainloop()

  def click_item(self, event):
    selectedItem = self.trv.focus()
    getValue = self.trv.item(selectedItem).get('values')  # 딕셔너리의 값만 가져오기
    str = f'{getValue[0]},{getValue[1]},{getValue[2]},{getValue[3]}'
    self.list.destroy()
    self.ctrl.forwardingControl('Info', str)
  def __init__(self):
    from analysisVisualization.database.controller.MemberController import MemberController
    self.controller = MemberController()
    self.dao = MemberDao()

    self.list = tkinter.Tk()
    self.list.title("Member List")

    font = tkinter.font.Font(family="맑은 고딕", size=30, slant="italic")
    self.lbl = tkinter.Label(self.list, text="Member List", font=font)
    self.lbl.config(bg="white")
    self.lbl.pack(side="top", fill="both", padx=10)

    # treeview 표
    self.trv = tkinter.ttk.Treeview(
      self.list, columns=["MemNo", "이름", "ID"],displaycolumns=["MemNo", "이름", "ID"])
    self.trv.pack(side="bottom", fill="both", padx=10, pady=10)

    self.trv.column("#0", width=100)
    self.trv.heading("#0", text="index", anchor="w")

    self.trv.column("#1", width=100, anchor="center")
    self.trv.heading("MemNo", text="MemNo", anchor="center")

    self.trv.column("#2", width=150, anchor="center")
    self.trv.heading("이름", text="이름", anchor="center")

    self.trv.column("#3", width=150, anchor="center")
    self.trv.heading("ID", text="ID", anchor="center")

    self.trv.bind('<ButtonRelease-1>', self.click_item)

    # db로 부터 들고 온 데이터를  TreeView에 담기
    trv_list = self.dao.get_all()
    if trv_list != None:
      for i in range(len(trv_list)):  # iid =index id
        self.trv.insert('', 'end', text=i, values=trv_list[i], iid=str(i) + ")")

    center_win(self.list, 540, 300)
    self.list.mainloop()

def click_item(self, event):
    selectedItem = self.trv.focus()
    getValue = self.trv.item(selectedItem).get('values')  # 딕셔너리의 값만 가져오기
    str = f'{getValue[0]},{getValue[1]},{getValue[2]},{getValue[3]}'
    self.list.destroy()
    self.ctrl.forwardingControl('Info', str)
728x90
반응형

댓글