[英]how to call a method from a class in python
如何在下面的代碼中從類Acceptor
調用bio_info
方法?
import sys
import Tkinter
from Tkinter import *
from tkMessageBox import *
from tkSimpleDialog import *
class Acceptor():
def bio_info(self):
#this method takes in input from the keyboard console
def__init__(self,name,mat_no,semester)
self.name=name
self.mat_no=mat_no
self.semester=semester
Label(top,text='Name').grid(row=0)
Label(top,text='Matric No').grid(row=1)
Label(top,text='Semester').grid(row=2)
name=Entry(top)
mat_no=Entry(top)
semester=Entry(top)
name.grid(row=0,column=1)
mat_no.grid(row=1,column=1)
semester.grid(row=2,column=1)
return name
return mat_no
return semester
main = Tk()
#This defines the size of the main window
main.geometry('640x480')
#This takes care of the configuration of the main window, buttons and labels inclusive
main.title('Result Calculator App')
mainLabel=Tkinter.Label(main,text='Result Calculator',bd=10, relief=RIDGE,fg='cyan')
mainLabel.pack(fill=BOTH)
mainLabel.config(font=('algerian',35, 'bold'), bg='blue',fg='orange')
calculate=Button(text="CALCULATE",font=('joan',20,'bold'),bg='black',fg='green',width=15,cursor='hand2',relief=SOLID,
command=Acceptor.bio_info)
calculate.pack()
print'This program ran correctly'
main.mainloop()
您可以像在 python 中的任何其他類中調用任何其他方法一樣調用它。 您創建該類的一個實例,然后在該實例上調用該方法:
acceptor = Acceptor()
...
calculate=Button(..., command=acceptor.bio_info, ...)
您需要創建該類的一個實例並從該實例調用該方法。 例子:
myAcceptor = Acceptor()
myAcceptor.bio_info()
正如 teckydesigner 所說,您需要先創建您的類的實例,然后才能使用它。 您的代碼command = Acceptor.bio_info
不會編譯,因為 bio_info 是一種方法,需要像這樣調用: myAcceptor.bio_info()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.