0
當我按下「gett」類中的按鈕時,我想調用car
類中的showallrecords1
函數。從按鈕命令中的另一個類別調用函數
我得到的錯誤
'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()
請閱讀https://stackoverflow.com/help/mcve。 –
[Python Tkinter GUI框架:如何從另一個類的函數內部調用一個類方法?](http://stackoverflow.com/questions/33795081/python-tkinter-gui-frame-how-to-從a函數中調用類方法) – WhatsThePoint
這不是你的代碼的相關部分。這是你的整個代碼...儘量最大限度地減少它的相關部分,如你所說。 –