請問遇到這個錯誤應該如何修改?AttributeError: 'windowclass1' object has no attribute 'windowclass'

82 views
Skip to first unread message

呂雅涵

unread,
Nov 17, 2017, 12:54:56 AM11/17/17
to python.tw
程式初學者,非資訊背景,請各位協助,感謝。

以下是我的程式碼

# -*- coding: utf-8 -*-
import sys
import tkinter.messagebox as box
from tkinter.filedialog import asksaveasfile
if sys.version_info[0] >= 3:
    import tkinter as tk
    import tkinter.ttk as ttk
else:
    import Tkinter as tk
import sqlite3
from tkinter import *

class windowclass(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)
        master.title("綠建築查詢系統")
        self.master.geometry("820x800")
        self.dict = {}

        self.dict1 = {}

        self.lb1 = tk.Label(master,text='依指標選擇')
        self.lb1.place(x=0,y=0)
        self.lb2 = tk.Label(master,text='依屬性選擇')
        self.lb2.place(x=200,y=0)
        self.lb3 = tk.Label(master,text='依變數值選擇')
        self.lb3.place(x=400,y=0)
        
        # Initialize comboboxes
        self.combobox_a = ttk.Combobox(master, values=list(self.dict.keys()), state='readonly')
        self.combobox_b = ttk.Combobox(master, values=self.dict['CO2減量指標'], state='readonly')
        self.combobox_c = ttk.Combobox(master, values=self.dict1['構造形式'], state='readonly')

        # Select 0 element
        self.combobox_a.current(0)
        self.combobox_b.current(0)
        self.combobox_c.current(0)

        # Add event to update variables on combobox's value change event
        self.combobox_a.bind("<<ComboboxSelected>>", lambda f1: self.fun())
        self.combobox_b.bind("<<ComboboxSelected>>", lambda f2: self.fun2())
        self.combobox_c.bind("<<ComboboxSelected>>", lambda f3: self.fun3())

        # Initialize variables
        self.area = self.combobox_a.get()
        self.last_area = self.country = self.combobox_b.get()
        self.last_country = self.twon = self.combobox_c.get()

        # Place all controls to frame
        self.combobox_a.place(x=0,y=25)
        self.combobox_b.place(x=180,y=25)
        self.combobox_c.place(x=360,y=25)

        self.lb4 = tk.Label(master,text='已選擇條件')
        self.lb4.place(x=0,y=60)
        self.listb1 = tk.Listbox(master,width=100)
        self.listb1.place(x=0,y=80)

        self.lb5 = tk.Label(master,text='前次選擇條件')
        self.lb5.place(x=0,y=260)
        self.listb2 = tk.Listbox(master,width=100)
        self.listb2.place(x=0,y=280)

        def ok():
            print (self.cb1_value.get())
            print  (self.cb2_value.get())
            print (self.cb3_value.get())

        self.btn = tk.Button(master , text = "加入" , command = ok )
        self.btn.place(x=640,y=250)
        
        self.btn1 = tk.Button(master , text = "查詢" , command =self.command)
        self.btn1.place(x=640,y=450)

    def fun(self):
        print("changed 1-st combobox value to: " + self.combobox_a.get())
        if self.last_area != self.combobox_a.get():
            self.combobox_b['values']=self.dict[self.combobox_a.get()]
            self.combobox_b.current(0)
            self.country = self.combobox_b.get()
        self.last_area = self.area = self.combobox_a.get()

    def fun2(self):
        print("changed 2-nd combobox value to: " + self.combobox_b.get())
        if self.last_country != self.combobox_b.get():
            self.combobox_c['values']=self.dict1[self.combobox_b.get()]
            self.combobox_c.current(0)
            self.twon = self.combobox_c.get()
        self.last_country = self.country = self.combobox_b.get()

    def fun3(self):
        print("changed 3-rd combobox value to: " + self.combobox_c.get())
        self.twon = self.combobox_c.get()

    def command(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = windowclass1(self.newWindow)

class windowclass0(tk.Frame):

    def __init__(self , master):
        tk.Frame.__init__(self, master)
        master.title("SUUMARY")
        self.master.geometry("820x800")
        self.quitButton2 = tk.Button(master, text = 'Quit', width = 25 , command = self.close_window2)
        self.quitButton2.place(x=400,y=250)
        self.frame2 = tk.Frame(master)
        self.frame2.place(x=0, y=10, width=800, height=650)

        #滚动条
        self.scrollBar2 = Scrollbar(self.frame2)
        self.scrollBar2.pack(side=RIGHT, fill=Y,)
        self.scrollBar3 = Scrollbar(self.frame2,orient=HORIZONTAL)
        self.scrollBar3.pack(side=BOTTOM, fill=X)

        self.tree2 = ttk.Treeview(self.frame2,
                    columns=('c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','c11','c12','c13','c14','c15'),
                    show="headings",
                    yscrollcommand=self.scrollBar2.set,
                    xscrollcommand=self.scrollBar3.set)
                
        #设置每列宽度和对齐方式
        self.tree2.column('c1', width=100, anchor='center')
        self.tree2.column('c2', width=150, anchor='center')
        self.tree2.column('c3', width=150, anchor='center')
        self.tree2.column('c4', width=150, anchor='center')
        self.tree2.column('c5', width=150, anchor='center')
        self.tree2.column('c6', width=180, anchor='center')
        self.tree2.column('c7', width=100, anchor='center')
        self.tree2.column('c8', width=100, anchor='center')
        self.tree2.column('c9', width=100, anchor='center')
        self.tree2.column('c10', width=100, anchor='center')
        self.tree2.column('c11', width=100, anchor='center')
        self.tree2.column('c12', width=100, anchor='center')
        self.tree2.column('c13', width=100, anchor='center')
        self.tree2.column('c14', width=100, anchor='center')
        self.tree2.column('c15', width=100, anchor='center')

        #设置每列表头标题文本
        self.tree2.heading('c1', text='編號')
        self.tree2.heading('c2', text='外殼設計方案')
        self.tree2.heading('c3', text='空調設計方案')
        self.tree2.heading('c4', text='照明設計方案')
        self.tree2.heading('c5', text='CO2減量方案')
        self.tree2.heading('c6', text='廢棄物減量方案')
        self.tree2.heading('c7', text='室內環境設計方案')
        self.tree2.heading('c8', text='生物多樣性方案')
        self.tree2.heading('c9', text='綠化量設計方案')
        self.tree2.heading('c10', text='基地保水設計方案')
        self.tree2.heading('c11', text='水資源節省方案')
        self.tree2.heading('c12', text='污水垃圾改善方案')
        self.tree2.heading('c13', text='總成本(元)')
        self.tree2.heading('c14', text='單位成本(元/坪)')
        self.tree2.heading('c15', text='單位成本(元/平方公尺)')
        
        self.tree2.pack(side=LEFT, fill=Y)
                
        #Treeview组件与垂直滚动条结合
        self.scrollBar2.config(command=self.tree2.yview)
        self.scrollBar3.config(command=self.tree2.xview)
        
        #插入演示数据
        conndb = sqlite3.connect("C:/data/outputnew.db")
        c = conndb.cursor()
        sql="""select * from '成本'"""
        for r in c.execute(sql):
            self.tree2.insert("","end",values=r)

    def close_window2(self):
            self.master.destroy()

class windowclass1(windowclass):

    def __init__(self , master ):
        self.master = master
        self.frame = tk.Frame(master)
        master.title("結果")
        self.master.geometry("1000x500")
        self.quitButton = tk.Button(master, text = 'Quit', width = 25 , command = self.close_window)
        self.quitButton.place(x=400,y=250)
        self.frame1 = tk.Frame(master)
        self.frame1.place(x=0, y=10, width=800)

        #滚动条
        self.scrollBar = Scrollbar(self.frame1)
        self.scrollBar.pack(side=RIGHT, fill=Y,)
        self.scrollBar1 = Scrollbar(self.frame,orient=HORIZONTAL)
        self.scrollBar1.pack(side=BOTTOM, fill=X)

        self.tree = ttk.Treeview(self.frame1,
                    columns=('c1','c2','c3','c4','c5','c6','c7'),
                    show="headings",
                    yscrollcommand=self.scrollBar.set,
                    xscrollcommand=self.scrollBar1.set)
                
        #设置每列宽度和对齐方式
        self.tree.column('c1', width=100, anchor='center')
        self.tree.column('c2', width=150, anchor='center')
        self.tree.column('c3', width=150, anchor='center')
        self.tree.column('c4', width=150, anchor='center')
        self.tree.column('c5', width=150, anchor='center')
        self.tree.column('c6', width=180, anchor='center')
        self.tree.column('c7', width=100, anchor='center')

        #设置每列表头标题文本
        self.tree.heading('c1', text='方案')
        self.tree.heading('c2', text='燈具系統')
        self.tree.heading('c3', text='燈具效率係數(IER)')
        self.tree.heading('c4', text='照明功率係數(IDR)')
        self.tree.heading('c5', text='再生能源節能比例(%)')
        self.tree.heading('c6', text='建築能源管理系統效率(Beta2)')
        self.tree.heading('c7', text='指標得分')
        self.tree.pack(side=LEFT, fill=Y)
                
        #Treeview组件与垂直滚动条结合
        self.scrollBar.config(command=self.tree.yview)
        self.scrollBar1.config(command=self.tree.xview)
        
        #插入演示数据
        
        conndb = sqlite3.connect('C:/data/outputnew.db')
        curr = conndb.cursor()
        curr.execute('select * from  "'+self.windowclass.self.combobox_a.get()+'" where "'+self.windowclass.self.combobox_b.get()+'" =  "'+self.windowclass.self.combobox_c.get()+'"')
        rows=curr.fetchall()
        for row in rows:
            self.tree.insert("","end",values=row)

    def close_window(self):
            self.master.destroy()

if __name__ == "__main__":
    root = tk.Tk()
    root2 = tk.Tk()
    app=windowclass(root)
    m=windowclass0(root2)
    app.mainloop()

在紅色那句出錯誤,不曉得如何解決,請幫幫忙。

Sonic Yang

unread,
Nov 17, 2017, 7:39:45 AM11/17/17
to pyth...@googlegroups.com
try this
'select * from  "' + self.windowclass.combobox_a.get() + '" where "' + self.windowclass.combobox_b.get() + '" =  "' + self.windowclass.combobox_c.get() + '"'
if still cannot work, please show the error msg.

Best regards
alingo

--
這是 Google 網上論壇針對「python.tw」群組發送的訂閱通知郵件。
如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電子郵件到 pythontw+unsubscribe@googlegroups.com
如要在這個群組張貼留言,請傳送電子郵件到 pyth...@googlegroups.com
請前往以下網址造訪這個群組:https://groups.google.com/group/pythontw
如需更多選項,請前往:https://groups.google.com/d/optout

呂雅涵

unread,
Nov 27, 2017, 9:52:00 AM11/27/17
to python.tw


python group於 2017年11月17日星期五 UTC+8下午8時39分45秒寫道:
如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電子郵件到 pythontw+u...@googlegroups.com

呂雅涵

unread,
Nov 27, 2017, 9:52:00 AM11/27/17
to python.tw
感謝您的回覆,已經測試過了,還是有錯誤
錯誤提示如下:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\呂雅涵\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "A:/Allen Chen/Python/3.x combobox-new-061118-三個視窗顯示成功.py", line 239, in command
    self.app = windowclass1(self.newWindow)
  File "A:/Allen Chen/Python/3.x combobox-new-061118-三個視窗顯示成功.py", line 537, in __init__
    curr.execute('select * from  "' + self.windowclass.combobox_a.get() + '" where "' + self.windowclass.combobox_b.get() + '" =  "' + self.windowclass.combobox_c.get() + '"')
AttributeError: 'windowclass1' object has no attribute 'windowclass'

python group於 2017年11月17日星期五 UTC+8下午8時39分45秒寫道:
如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電子郵件到 pythontw+u...@googlegroups.com

如要在這個群組張貼留言,請傳送電子郵件到 pyth...@googlegroups.com
請前往以下網址造訪這個群組:https://groups.google.com/group/pythontw
如需更多選項,請前往:https://groups.google.com/d/optout


python group於 2017年11月17日星期五 UTC+8下午8時39分45秒寫道:
如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電子郵件到 pythontw+u...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages