在button命令中从另一个类调用函数

时间:2017-02-14 10:43:19

标签: python tkinter

当我在“gett”类中按下按钮时,我想从showallrecords1类调用car函数。

我收到错误

'gett' object has no attribute 'car'

以下是我的代码的相关部分

from tkinter import *
import tkinter as tk
import sqlite3

conn = sqlite3.connect('jadval.db')
c = conn.cursor()

class App:
    def __init__(self,master):
        frame = Frame(master)
        frame.pack()

##        bottomframe = Frame(master)
##        bottomframe.pack( side = BOTTOM )

        self.master=master

        a= StringVar()
        b= StringVar()
        a1= StringVar()
        b1= StringVar()
        c= StringVar()

        self.button = Button(frame,text="memory", command=self.ouvrir)
        self.button.pack(side=LEFT)

        self.button2 = Button(frame,text="report", command=self.tabluh)
        self.button2.pack(side=LEFT)

        self.button3 = Button(frame,text="setting", command=self.fermer)
        self.button3.pack(side=LEFT)

        self.button20 = Button(frame,text="calibration", command=self.cal)
        self.button20.pack(side=LEFT)


        self.button30 = Button(frame,text="empty weight", command=self.weighing)
        self.button30.pack(side=LEFT)

    def ouvrir(self):
        root3=Toplevel(self.master)
        mygui=records(root3)


    def tabluh(self):
        root4=Toplevel(self.master)
        mygui2=report(root4)



    def fermer(self):
        root5=Toplevel(self.master)
        mygui3=settting(root5) 

    def cal(self):
        root8=Toplevel(self.master)
        mygui8=cal(root8)  
    def weighing(self):
        root9=Toplevel(self.master)
        mygui9=gett(root9)

class car():####################################################  trunck manage

    def __init__(self,master):

        ##super(car,self).__init__()
##        tk.Frame.__init__(self, master)

        self.master=master
##        self.master.geometry('250x200+100+200')
        self.master.geometry('400x300')
        self.master.title('Report')
        self.button28 = Button(self.master,text="create", command=self.tabluh1)
        self.button28.grid(row=0, column=0)

        self.button29 = Button(self.master,text="insert rec", command=self.insertar1)
        self.button29.grid(row=3, column=2)

        self.tyLabel1 = Label(self.master,text="driver name")
        self.tyLabel1.grid(row=1, column=0)

        self.disLabel1 = Label(self.master,text="trunck type")
        self.disLabel1.grid(row=2, column=0)

        self.pelLabel1 = Label(self.master,text="pelack")
        self.pelLabel1.grid(row=3, column=0)


        self.d = Entry(self.master) 
        self.d.grid(row=1, column=1)

        self.e = Entry(self.master) 
        self.e.grid(row=2, column=1)

        self.f = Entry(self.master) 
        self.f.grid(row=3, column=1)

        self.connection = sqlite3.connect('jadval.db')
        self.cur = self.connection.cursor()
        self.dateLabel1 = Label(self.master, text="driver name", width=10)
        self.dateLabel1.grid(row=4, column=0)
        self.BMILabel1 = Label(self.master, text="trunck type", width=10)
        self.BMILabel1.grid(row=4, column=1)
        self.stateLabel1 = Label(self.master, text="pelack", width=10)
        self.stateLabel1.grid(row=4, column=2)
        self.showallrecords1()

    def showallrecords1(self):

        data1 = self.readfromdatabase1()
        for index1, dat1 in enumerate(data1):
            Label(self.master, text=dat1[0]).grid(row=index1+5, column=0)
            Label(self.master, text=dat1[1]).grid(row=index1+5, column=1)
            Label(self.master, text=dat1[2]).grid(row=index1+5, column=2)

    def tabluh1(self):

        c.execute('''CREATE TABLE trunmanage(id1 INTEGER,firs1 stringvar(10),las1 stringvar(10))''')
    def insertar1(self):

        d1=self.d.get()
        e1=self.e.get()
        f1=int(self.f.get())
        c.execute("INSERT INTO trunmanage (id1, firs1,las1 ) VALUES (?, ?, ?)",(f1, d1, e1))
        conn.commit()
        self.showallrecords1()

    def readfromdatabase1(self):
        self.cur.execute("SELECT * FROM trunmanage")
        return self.cur.fetchall()


class gett():########################################### class selecting trunck from empty weight window

    def __init__(self,master):

        ##super(gett,self).__init__()
##        tk.Frame.__init__(self, master)

        self.master=master
##        self.master.geometry('250x200+100+200')
        self.master.geometry('200x120')
        self.master.title('gett')

##        obj2 = car()
##        b1 = Button(master,text="button 1",command=obj2.showallrecords1)
##        b1.pack()

##        self.button32 = Button(text='select', command=self.master.showallrecords1)
##        self.button32.pack()    
        self.button32 = Button(self.master,text="select trunck", command=self.car.showallrecords1);
        self.button32.pack(side=LEFT)
root = Tk()
root.title("Dbase")
root.geometry('500x120')


app=App(root)
root.mainloop()

1 个答案:

答案 0 :(得分:2)

gett类没有名为“car”的成员。你必须在类定义中的某个地方定义它。 I. e。:

self.car = car(master)