简体   繁体   中英

Calling different functions inside a class

I'm having a question related to functions in a class. My goal is to write a program with a Visualization class, which handles all the GUI related stuff with tkinter and another class called SensorHandling , which will mainly test sensors whether they work or not and return the result, which will then be displayed in Visualization . I'm having problems with my GUI class.

I have two different functions inside the class Visualization, namely StartPage and TestingHead_Page . I pass root in order for them to be able to modify my application directly. Now, I want to display the TestingHead_Page with my button bTestingHead , but I get an error that TestingHead_Page is not defined.

How do I go about this problem? Criticism is always appreciated as I want to learn.

import tkinter as tk
from tkinter import ttk

import serial
import platform
import time

LARGEFONT = ("Verdana", 35)

class Visualization:

    def StartPage(self, root):

        lTitle= ttk.Label(root, text ="Title", font = LARGEFONT)
        lTitle.grid(row = 0, column = 1, sticky = "N", padx = 10, pady = 10)
        lTitle.config(font = ('Calibri', 32, 'bold'))

        lTesting = ttk.Label(root, text = "Testing", font = ('calibri', 20))
        lTesting.grid(row = 1, column = 0, padx = 10, pady = 10)

        bTestingHead = ttk.Button(root, text = "Testing Head", command = self.TestingHead_Page(root))
        bTestingHead.grid(row = 2 , column = 0, padx = 20, pady = 20)

        exit = tk.PhotoImage(file = "exit_icon.png")
        bExit = ttk.Button(root, text = "exit", image = exit, command = root.destroy)
        bExit.image = exit
        bExit.place(relx = 1.0, rely = 0.0, anchor = 'ne')

    def TestingHead_Page(self, root):

        lTitle = ttk.Label(root, text = "Testing Head")
        lTitle.grid(row = 0, column = 0, sticky = "W", padx = 10, pady = 10)

        bStartPage = ttk.Button(root, text = "Start Page", command = "")
        bStartPage.grid(row = 0, column = 1, sticky = "E", padx = 10, pady = 10)

        exit = tk.PhotoImage(file = "exit_icon.png")
        bExit = ttk.Button(root, text = "exit", image = exit, command = root.destroy)
        bExit.image = exit
        bExit.place(relx = 1.0, rely = 0.0, anchor = 'ne')

    if __name__ == "__main__":

        root = tk.Tk()
        guiref = StartPage(self, root)
        tk.mainloop()

#class SensorHandling():

In python, you need to pass the current instance, referenced through the self keyword, explicitly in the class's methods:

def StartPage(self, root):
    ...

def TestingHead_Page(self, root):
    ...

While inside the class, you would call them like this:

self.StartPage(root)
self.TestingHead_Page(root)

Read up on classes in Python .

Also, from the code, it seems like TestingHead_Page should return some value since you try to capture that in command = TestingHead_Page(root) , so you need to fix that as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM