Python3 - tkinter 导入和使用模块

Posted

技术标签:

【中文标题】Python3 - tkinter 导入和使用模块【英文标题】:Python3 - tkinter importing and using module 【发布时间】:2015-01-20 17:31:00 【问题描述】:

我在导入和使用我创建的模块时遇到问题。我有 patcher.py,我想从 patch.py​​ 导入模块,但在尝试导入和使用 disable_removecd 时出现错误。我现在对如何正确设置它以及如何正确导入和使用它有点困惑。

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) 

补丁.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

我做错了什么?

【问题讨论】:

【参考方案1】:

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)

patches.py 中的函数disable_removecd 应修改为接受cmexecutable 作为参数:

def disable_removecd(cmexecutable):
    ....

【讨论】:

我进行了这些更改,但是当我单击 bttn1 时,我得到:File "cmpatcherv002.py", line 37, in <lambda> self.bttn1['command'] = lambda: disable_removecd(cmexecutable) TypeError: disable_removecd() takes 0 positional arguments but 1 was given @José,我忘了提到disable_removecd 也应该被修改。我更新了答案以包含它。 现在似乎可以工作了。我更改了def disable_removecd(self):open(self, 'r+b') as victim: 这是正确的吗?我也应该使用self.offset1 吗? @José, self 不是参数的推荐名称,因为self 用作实例方法的第一个参数。但是函数disable_removecd不是方法。 @José,你不需要限定offset1offset2,...因为它们是同一个函数中的局部变量。

以上是关于Python3 - tkinter 导入和使用模块的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu下Python3导入EasyGui模块

tkinter模块常用参数(python3)

python3 tkinter 桌面软件教程

tkinter模块常用参数(python3)

tkinter模块常用参数(python3)

Jupyter vs IPython:Matplotlib - 没有名为 Tkinter 的模块