Python Tkinter GUI Frame:如何从另一个类的函数内部调用类方法? [英] Python Tkinter GUI Frame: How to call a class method from inside a function of another class?

查看:36
本文介绍了Python Tkinter GUI Frame:如何从另一个类的函数内部调用类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的程序创建登录脚本.我能够编写一个基本的脚本来测试我的登录框架,但是我现在在我基本上登录后能够访问另一个框架.

I am trying to create a login script for my program. I was able to write a basic script to test out my Login Frame but I now what to be able to access another Frame after I essentially login.

这是我的脚本的一部分:

Here is part of my script:

#!/usr/bin/python


from tkinter import *
from tkinter import ttk

import tkinter as tk
import tkinter.messagebox as tm


Large_Font = ("Verdana", 18)
Small_Font = ("Verdana", 12)


class ATM(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "ATM Simulator")

        container = tk.Frame(self)
        container.pack(side = "top", fill ="both", expand =True)
        container.grid_rowconfigure(100, weight=1)
        container.grid_columnconfigure(100, weight=1)

        #Create Frame Library and use For Loop to switch between frames

        self.frames = {}

        for i in (LogIn, WelcomePage, Checking, Savings, Transfer):

            frame = i(container, self)
            self.frames[i] = frame 
            frame.grid(row= 100, column = 100, sticky= "nsew")

        self.show_frame(LogIn)

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

class LogIn(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        global act_num_entry
        global pin_num_entry

        label = Label(self, text = "Login Using Account and PIN Numbers", font=Small_Font)
        label.pack(pady=50, padx=50)

        act_num_label = Label(self, text="Account Number")
        act_num_entry = Entry(self)

        pin_num_label = Label(self, text="PIN Number")
        pin_num_entry = Entry(self, show="*")

        act_num_label.pack(pady=5, padx=5)
        pin_num_label.pack(pady=5, padx=5)

        act_num_entry.pack(pady=5, padx=5)
        pin_num_entry.pack(pady=5, padx=5)


        logBTN = ttk.Button(self, text="Enter", 
                            command =self.log_check)
        logBTN.pack()

        quitButton = ttk.Button(self, text = "End Transaction", command = quit)
        quitButton.pack()

    def log_check(self):

        #default test pin and account numbers 

        act_num=1234567
        pin_num=1234   

        #This Try/Except handles the non-integer values being entered

        try:
            actNum = int(act_num_entry.get())
            pinNum = int(pin_num_entry.get())
        except:
            tm.showerror("Login Error", "Invalid Entry")
            pass

        if actNum == act_num and pinNum == pin_num:

            #Message Window only used to to prove my log_check function works

            tm.showinfo("ATM Login", "Login Successful")


            '''Insert script to open the WelcomePage(tk.Frame) method'''


        elif actNum != act_num:
            tm.showerror("Login Error", "Invalid Account Number")
        elif pinNum != pin_num:
            tm.showerror("Login Error", "Invalid PIN Number")
        else:
            tm.showerror("Login Error", "Invalid Entry")

class WelcomePage(tk.Frame):

    #Welcome Page Window

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = Label(self, text = "Welcome to the ATM Simulator", font = Large_Font)
        label.pack(pady=100, padx=100)

        checkButton = ttk.Button(self, text = "Checking Account", 
                             command = lambda: controller.show_frame(Checking))
        checkButton.pack()

        saveButton = ttk.Button(self, text = "Savings Account", 
                            command = lambda: controller.show_frame(Savings))
        saveButton.pack()

        transButton = ttk.Button(self, text = "Transfer Funds", 
                            command = lambda: controller.show_frame(Transfer))
        transButton.pack()

        quitButton = ttk.Button(self, text = "End Transaction", command = self.client_exit)
        quitButton.pack()

    def client_exit(self):
        exit()

所以我想从我的内部调用我的 WelcomePage(tk.Frame) 方法if actNum == act_num 和 pinNum == pin_num: 函数所以我基本上可以登录到我的程序.我尝试使用我的 show_frame 函数访问 WelcomePage(tk.Frame) 但我无法访问,因为我知道该函数是 ATM(tk.Tk) 类不是 LogIn(tk.Frame).这有可能以我想要的方式完成还是我将不得不编写另一个登录脚本来完成此任务?

So I want to call my WelcomePage(tk.Frame) method from inside my if actNum == act_num and pinNum == pin_num: function so I can essentially login to my program. I tried to access the WelcomePage(tk.Frame)using my show_frame function but I was not able to because I understand that the function is apart of the ATM(tk.Tk) class not LogIn(tk.Frame). Is this possible to accomplish the way I want to or am I going to have to write another login script to accomplish this?

推荐答案

show_frame 是控制器上的一个方法.您只需要保存对控制器的引用,并从任何地方调用它.这就是控制器类的全部目的 - 控制对其他窗口的访问.

show_frame is a method on the controller. You simply need to save a reference to the controller, and call it from anywhere. This is the entire purpose of the controller class - to control access to the other windows.

第一步是修改你的类以保存对控制器的引用:

The first step is to modify your classes to save a reference to the controller:

class Login(tk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        ...

class WelcomePage(tk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        ...

现在,您可以随时随地调用show_frame:

Now, you can call show_frame wherever you want:

if actNum == act_num and pinNum == pin_num:
    ...
    self.controller.show_frame(WelcomePage)
    ...

有关控制器的更多信息,请参阅此答案:https://stackoverflow.com/a/32865334/7432

For more information on the controller see this answer: https://stackoverflow.com/a/32865334/7432

这篇关于Python Tkinter GUI Frame:如何从另一个类的函数内部调用类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆