2

我正在开发一个小程序,可以用作化学计算器或类似的东西,但在此过程中我遇到了一些麻烦......

在主文件中,我试图从文件 grafika.py 中导入类 Aplikacija,但无论我多么努力,它似乎都不起作用 - 我得到 AttributeError: 'module' object has no attribute 'Aplikacija' .

这可能是初学者的问题,但我已经盯着屏幕看了几个小时,我不知道可能出了什么问题。如果您能帮助我,我将不胜感激:)

PS:请不要介意变量的名称,因为我的母语不是英语...

主文件的(缩短的)代码:

import grafika
from Tkinter import *

root = Tk()
root.title('ChemHelp v0.1')
root.minsize(360, 240)
#root.iconbitmap('mojaikona.ico')

p = grafika.Aplikacija(root)

root.mainloop()

这是整个 grafika.py 文件(它很长......)

from Tkinter import *
import webbrowser

class Aplikacija():

    def __init__(self, master):

        #meni
        self.meni = Menu(master)
        master.config(menu=self.meni)
        self.meni.add_command(label="Periodni sistem", command=self.ps_okno)

        self.help_meni = Menu(self.meni)
        self.meni.add_cascade(label="Pomoč", menu=self.help_meni)
        self.help_meni.add_command(label="PDF", command=self.open_pdf)
        self.help_meni.add_command(label="Spletna dokumentacija", command=self.open_web)

        self.meni.add_command(label="O programu", command=self.open_about)
        self.meni.add_command(label="Zahvale", command=self.open_thanks)
        self.meni.add_command(label="Zapri", command=quit)

        #glavno okno
        body = Frame(root, width=360, height=240)
        body.pack()

        #izracun molske mase
        self.okno_masa = LabelFrame(body, text="Molska masa", padx=5, pady=5)
        self.okno_masa.grid(padx=10, pady=10)
        self.prompt1 = Label(self.okno_masa, text="Formula spojine: ")
        self.prompt1.grid(row=0, sticky=W)
        self.formula1 = Entry(self.okno_masa)
        self.formula1.grid(row=0, column=1)
        self.send1 = Button(self.okno_masa, text="Pošlji", command=lambda: self.gfx_molska_masa(self.okno_masa))
        self.send1.grid(row=1, columnspan=2)

        #izracun mnozine
        self.okno_mn = LabelFrame(body, text="Množina", padx=5, pady=5)
        self.okno_mn.grid(padx=10, pady=10)
        self.prompt2 = Label(self.okno_mn, text="Formula spojine: ")
        self.prompt2.grid(row=4, sticky=W)
        self.formula2 = Entry(self.okno_mn)
        self.formula2.grid(row=4, column=1)
        self.prompt3 = Label(self.okno_mn, text="Masa spojine: ")
        self.prompt3.grid(row=5, sticky=W)
        self.masa = Entry(self.okno_mn)
        self.masa.grid(row=5, column=1)
        self.send2 = Button(self.okno_mn, text="Pošlji", command=lambda: self.gfx_mnozina(self.okno_mn))
        self.send2.grid(row=6, columnspan=2)

    #funkcije
    def ps_okno(self):
        self.ps = Toplevel()
        self.ps.title("Periodni sistem")
        self.ps.minsize(480, 180)
        self.tekst = Label(self.ps, text="Tukaj bo periodni...")
        self.tekst.grid()
        self.zapri = Button(self.ps, text="Zapri", command=self.ps.destroy)
        self.zapri.grid()
        print("Okno odprto :P")

    def open_pdf(self):
        print("PDF odprt ;)")

    def open_web(self):
        self.url = 'http://test.org'
        webbrowser.open_new(self.url)
        print("Splet prečekiran :D")

    def open_about(self):
        self.o = Toplevel()
        self.o.title("O programu")
        self.o.minsize(300, 300)
        self.about = LabelFrame(self.o, text="O programu", padx=5, pady=5)
        self.about.grid(padx=10, pady=10)
        self.slika = PhotoImage(file="about_slika.gif")
        self.photo = Label(self.about, image=self.slika)
        self.photo.slika=self.slika
        self.photo.grid(row=0, columnspan=2)
        self.avtor = Label(self.about, text="Avtor: ")
        self.avtor.grid(row=1, sticky=W)
        self.avtor2 = Label(self.about, text="Jane Doe")
        self.avtor2.grid(row=1, column=1)
        self.verzija = Label(self.about, text="Verzija programa: ")
        self.verzija.grid(row=2, sticky=W)
        self.verzija2 = Label(self.about, text="1.0")
        self.verzija2.grid(row=2, column=1)
        self.jezik = Label(self.about, text="Pisano v programskem jeziku Python. © 2013")
        self.jezik.grid(row=3, columnspan=2)
        self.zapri = Button(self.about, text="Zapri", command=self.o.destroy)
        self.zapri.grid(row=4, columnspan=2)
        print("Kdo bo to sploh bral? :O")

    def open_thanks(self):
        self.h = Toplevel()
        self.h.title("Zahvale")
        self.h.minsize(200, 200)
        self.zahvale = LabelFrame(self.h, text="Zahvale", width=100, padx=5, pady=5)
        self.zahvale.grid(padx=10, pady=10)
        self.z_vsebina = Label(self.zahvale, text="Rada bi se zahvalila bla bla... in tle pol ful teksta al neki...")
        self.z_vsebina.grid()
        self.mentor = LabelFrame(self.h, text="Mentor", width=100, padx=5, pady=5)
        self.mentor.grid(padx=10, pady=10)
        self.m_vsebina = Label(self.mentor, text="John Does, prof.")
        self.m_vsebina.grid()
        self.viri = LabelFrame(self.h, text="Viri", width=100, padx=5, pady=5)
        self.viri.grid(padx=10, pady=10)
        self.v_vsebina = Label(self.viri, text="http://test.org \nhttp://www.test.com")
        self.v_vsebina.grid()
        self.zapri = Button(self.h, text="Zapri", command=self.h.destroy)
        self.zapri.grid()
        print("Tale stvar je lame -.-")

    def gfx_molska_masa(self, frame):
        self.rezultat = Entry(frame)
        self.rezultat.config(width="9")
        self.rezultat.grid(row=3)
        self.molska_masa = "187"
        self.rezultat.insert(0, self.molska_masa)
        self.enota = Label(frame, text="g/mol")
        self.enota.grid(row=3, column=1, sticky=W)
        print("Molska masa je")

    def gfx_mnozina(self, frame):
        self.rezultat = Entry(frame)
        self.rezultat.config(width="8")
        self.rezultat.grid(row=7)
        self.mnozina = "1.0"
        self.rezultat.insert(0, self.mnozina)
        self.enota = Label(frame, text="mol")
        self.enota.grid(row=7, column=1, sticky=W)
        print("Mnozina je")

当我从 grafika.py 文件中的主文件运行代码时,它工作正常,没有错误或任何东西......

4

1 回答 1

1

如果您已经导入了一次文件,则另一个导入不会重新加载它 - 模块被缓存。您需要获取reload(grafika)最新的更改。

于 2012-10-30T19:13:11.640 回答