2014-11-22 115 views
0

我無法導入和使用我創建的模塊。我有patcher.py,我想從patches.py導入模塊,但在嘗試導入並使用disable_removecd時出現錯誤。我現在對如何正確設置以及如何正確導入和使用它有點困惑。Python3 - tkinter導入和使用模塊

patcher.py

#import the tkinter module 
from tkinter import * 
from tkinter.filedialog import askopenfilename 
import bsdiff4 
from patches import * 

#bsdiff4.file_patch(dst, dst, patch) 

#create a new class 
class Application(Frame): 
    def __init__(self, master): 
     super(Application, self).__init__(master) 
     self.grid(row = 2, sticky = W+E+N+S) 
     #,padx=300 
     cmexecutable = askopenfilename() 
     print(cmexecutable) 
     self.mainmenu() 

    def mainmenu(self): 

     self.logo = PhotoImage(file='logo.gif') 
     self.image = Label(self, image=self.logo) 
     self.image.grid(columnspan = 2) 
     self.image.configure(background='black') 

     #self.bttn1 = Button(self, text = 'Country Specific') 
     self.bttn1 = Button(self, text = 'Disable Remove CD Message') 
     self.bttn1['command'] = disable_removecd(self) 
     self.bttn1.grid(columnspan = 2 ,sticky = W+E+N+S) 

patches.py

from patcher import * 

def disable_removecd(): 

    offset1 = 0x42a98b 
    offset2 = 0x42a98c 
    offset3 = 0x42a98d 
    offset4 = 0x42a98e 
    offset5 = 0x42a98f 
    offset6 = 0x42e400 
    offset7 = 0x42e401 
    offset8 = 0x42e402 
    offset9 = 0x42e403 
    offset10 = 0x42e404 

    newvalue1 = b'\x90' 
    newvalue2 = b'\x90' 
    newvalue3 = b'\x90' 
    newvalue4 = b'\x90' 
    newvalue5 = b'\x90' 
    newvalue6 = b'\x90' 
    newvalue7 = b'\x90' 
    newvalue8 = b'\x90' 
    newvalue9 = b'\x90' 
    newvalue10 = b'\x90' 

    with open(cmexecutable, 'r+b') as victim: 
     victim.seek(offset1) 
     victim.write(newvalue1) 
     victim.seek(offset2) 
     victim.write(newvalue2) 
     victim.seek(offset3) 
     victim.write(newvalue3) 
     victim.seek(offset4) 
     victim.write(newvalue4) 
     victim.seek(offset5) 
     victim.write(newvalue5) 
     victim.seek(offset6) 
     victim.write(newvalue6) 
     victim.seek(offset7) 
     victim.write(newvalue7) 
     victim.seek(offset8) 
     victim.write(newvalue8) 
     victim.seek(offset9) 
     victim.write(newvalue9) 
     victim.seek(offset10) 
     victim.write(newvalue10) 

當我運行patcher.py我得到這個錯誤:

self.bttn1['command'] = disable_removecd(self) 
NameError: name 'disable_removecd' is not defined 

我在做什麼錯?

回答

0

patches.py中的以下行會導致循環導入。

from patcher import * 

我想你用這條線在disable_removecd功能使用cmexecutable。刪除上面的行。並明確傳遞cmexecutable的值。

patcher.py

... 

class Application(Frame): 

    def __init__(self, master): 
     super(Application, self).__init__(master) 
     self.grid(row=2, sticky=W+E+N+S) 
     cmexecutable = askopenfilename() 
     self.mainmenu(cmexecutable) 

    def mainmenu(self, cmexecutable): 
     self.logo = PhotoImage(file='logo.gif') 
     self.image = Label(self, image=self.logo) 
     self.image.grid(columnspan=2) 
     self.image.configure(background='black') 

     self.bttn1 = Button(self, text = 'Disable Remove CD Message') 
     self.bttn1['command'] = lambda: disable_removecd(cmexecutable) 
     # ^^ set up callback instead of calling it immediately using `lambda`. 
     self.bttn1.grid(columnspan=2 ,sticky=W+E+N+S) 

功能disable_removecdpatches.py應加以修改,以接受cmexecutable作爲參數:

def disable_removecd(cmexecutable): 
    .... 
+0

我做出這些改變,但是當我點擊bttn1我得到:'文件「cmpatcherv002.py」,第37行,在 self.bttn1 ['c ommand'] = lambda:disable_removecd(cmexecutable) TypeError:disable_removecd()需要0個位置參數,但是有1個被給出了' – 2014-11-22 18:07:07

+0

@José,我忘了提及'disable_removecd'也應該被修改。我更新了包含它的答案。 – falsetru 2014-11-22 18:08:49

+0

似乎現在就工作。我把'def disable_removecd(self):'和'open(self,'r + b')改爲受害者:'這是正確的嗎?我也應該使用'self.offset1'? – 2014-11-22 18:16:09