2017-10-29 8 views
0

내 프로그램이 탭과 같은 프레임을 변경하지 않는 이유는 무엇입니까?Python Tkinter 프레임

내 LoginPage는 사용자와 암호를 올바르게 확인하지만 유효성 검사 후 주 프로그램으로 어떻게 변경할 수 있는지 알지 못합니다.

import tkinter as tk 
from tkinter import ttk 
import dbm 

class Program(tk.Tk): 
    def __init__(self, *args,**kwargs): 
     tk.Tk.__init__(self,*args,**kwargs) 
     container = tk.Frame(self) 
     container.pack(side='top',fill='both',expand=True) 
     container.grid_rowconfigure(0,weight=1) 
     container.grid_columnconfigure(0,weight=1) 

     self.frames = {} 
     Frames = (LoginPage, StartPage) 
     for F in Frames: 
      frame = F(container, self) 
      self.frames[F] = frame 
      frame.grid(row=0, column = 0, sticky="nsew") 

     self.ShowF(LoginPage) 

    def ShowF(self, cont): 
     frame = self.frames[cont] 
     frame.tkraise() 

class LoginPage(tk.Frame): 
    def __init__(self,parent,controller): 
     tk.Frame.__init__(self,parent) 
     stats = tk.Label(self, text = 'Insira os dados para a validação') 
     stats.pack() 
     lab = tk.Label(self, text = ('Usuário')) 
     lab.pack() 
     self.ent = tk.Entry(self) 
     self.ent.pack() 
     lab2 = tk.Label(self, text = ('Senha')) 
     lab2.pack() 
     self.ent2 = tk.Entry(self, show='*') 
     self.ent2.pack() 
     but = tk.Button(self, text = 'Validar', command = self.Validacao) 
     but.pack() 
     self.lab3 = tk.Label(self, text = '') 
     self.lab3.pack() 

    def Validacao(self): 
     user = self.ent.get() 
     passw = self.ent2.get() 
     with dbm.open('files/contas','rb') as contas: 
      accv = [key1.decode() for key1 in contas.keys()] 
      passv = [contas[key].decode() for key in contas.keys()] 
      while True: 
       try: 
        k = accv.index(user) 
        k1 = passv.index(passw) 
        break 
       except: 
        self.lab3['text'] = ('Usuário e/ou senha inválidos!') 
        return 
      if k == k1: 
       self.lab3['text'] = ('Validação concluída!') 
       lambda :controller.ShowF(StartPage) #The problem is here(I think) 

class StartPage(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self,parent) 
     label = tk.Label(self, text="Start Page") 
     label.pack(pady=10, padx=10) 

     button = tk.Button(self, text="Button1") 
     button.pack() 
     buttona = tk.Button(self, text="Button2") 
     buttona.pack() 

app = Program() 
app.mainloop() 
+0

프로그램이 어떻게 작동해야하는지 자세히 설명해 주시겠습니까? – Nae

+0

로그인 화면 (로그인 페이지)에서 사용자와 암호를 확인한 다음 주요 기능 페이지 (anotherPage)에 주요 기능을 입력하십시오. –

답변

1

lambda :controller.ShowF(StartPage)은 함수를 호출하지 않는 새로운 함수를 반환합니다. 당신은 lambda를 제거해야합니다 또한, 코드가 필요

self.controller.ShowF(StartPage) 

controller에 대한 참조를 저장 : self.var = 람다 : : 문제는 VAR을 만들어 해결했다

class LoginPage(tk.Frame): 
    def __init__(self,parent,controller): 
     self.controller = controller 
     ... 
+0

Tkinter 콜백에서 예외가 발생했습니다. Traceback (최근 호출 마지막) : 파일 "C : \ 사용자 \ ThomasCaio \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ tkinter \ __ init__.py ", 줄 1699, __call__ return self.func (* args) 파일"C : \ Users \ ThomasCaio \ Documents \ Python \ File2.py ", Validacao의 라인 60, controller.ShowF (시작 페이지) NameError : 이름 'controller'가 정의되지 않았습니다. –

+0

@ThomasCaio : 네, 그게 다른 문제입니다. 컨트롤러에 대한 참조를'self.controller'로 저장 한 다음 호출을'self.controller.ShowF (StartPage)'로 만들어야합니다. 요점은,'lambda'는 함수를 호출하지 않고 단순히 새로운 함수를 생성한다는 것입니다. 그래서 코드가 아무 것도하지 않는 것입니다. –

+0

감사합니다 !!!!!!!!! –

0

controller.ShowF을 (시작 페이지) 다음 버튼 텍스트를 변경하고 [ '명령'] 버튼 = self.var