我一直在用这个程序走到尽头。我正在尝试构建一个类,让我在使用时控制BIP的按钮。到目前为止,这就是我所拥有的(见下文)。它继续运行这个奇怪的错误TypeError: 'module' object is not callable
- 我,来自C ++和C#(出于某种原因,#include...
更容易),不知道这意味着什么,谷歌没有帮助,所以...我知道我需要一些真正的语法等帮助 - 任何事情都会有所帮助。我知道这里有很多很多的问题,我真的很感激帮助!
注意:找到的基本代码here用于为此“自定义按钮类”创建骨架
import wx
from wxPython.wx import *
class Custom_Button(wx.PyControl):
# The BMP's
# AM I DOING THIS RIGHT? - I am trying to get empty 'global'
# variables within the class
Mouse_over_bmp = None #wxEmptyBitmap(1,1,1) # When the mouse is over
Norm_bmp = None #wxEmptyBitmap(1,1,1) # The normal BMP
Push_bmp = None #wxEmptyBitmap(1,1,1) # The down BMP
Pos_bmp = wx.Point(0,0) # The posisition of the button
def __init__(self, parent, NORM_BMP, PUSH_BMP, MOUSE_OVER_BMP,
pos, size, text="", id=-1, **kwargs):
wx.PyControl.__init__(self,parent, id, **kwargs)
# The conversions, hereafter, were to solve another but. I don't know if it is
# necessary to do this since the source being given to the class (in this case)
# is a BMP - is there a better way to prevent an error that i have not
# stumbled accost?
# Set the BMP's to the ones given in the constructor
self.Mouse_over_bmp = wx.Bitmap(wx.Image(MOUSE_OVER_BMP, wx.BITMAP_TYPE_ANY).ConvertToBitmap())
self.Norm_bmp = wx.Bitmap(wx.Image(NORM_BMP, wx.BITMAP_TYPE_ANY).ConvertToBitmap())
self.Push_bmp = wx.Bitmap(wx.Image(PUSH_BMP, wx.BITMAP_TYPE_ANY).ConvertToBitmap())
self.Pos_bmp = self.pos
self.Bind(wx.EVT_LEFT_DOWN, self._onMouseDown)
self.Bind(wx.EVT_LEFT_UP, self._onMouseUp)
self.Bind(wx.EVT_LEAVE_WINDOW, self._onMouseLeave)
self.Bind(wx.EVT_ENTER_WINDOW, self._onMouseEnter)
self.Bind(wx.EVT_ERASE_BACKGROUND,self._onEraseBackground)
self.Bind(wx.EVT_PAINT,self._onPaint)
self._mouseIn = self._mouseDown = False
def _onMouseEnter(self, event):
self._mouseIn = True
def _onMouseLeave(self, event):
self._mouseIn = False
def _onMouseDown(self, event):
self._mouseDown = True
def _onMouseUp(self, event):
self._mouseDown = False
self.sendButtonEvent()
def sendButtonEvent(self):
event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, self.GetId())
event.SetInt(0)
event.SetEventObject(self)
self.GetEventHandler().ProcessEvent(event)
def _onEraseBackground(self,event):
# reduce flicker
pass
def _onPaint(self, event):
dc = wx.BufferedPaintDC(self)
dc.SetFont(self.GetFont())
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
dc.Clear()
dc.DrawBitmap(self.Norm_bmp)
# draw whatever you want to draw
# draw glossy bitmaps e.g. dc.DrawBitmap
if self._mouseIn: # If the Mouse is over the button
dc.DrawBitmap(self, self.Mouse_over_bmp, self.Pos_bmp, useMask=False)
if self._mouseDown: # If the Mouse clicks the button
dc.DrawBitmap(self, self.Push_bmp, self.Pos_bmp, useMask=False)
import wx
import Custom_Button
from wxPython.wx import *
ID_ABOUT = 101
ID_EXIT = 102
class MyFrame(wx.Frame):
def __init__(self, parent, ID, title):
wxFrame.__init__(self, parent, ID, title,
wxDefaultPosition, wxSize(400, 400))
self.CreateStatusBar()
self.SetStatusText("Program testing custom button overlays")
menu = wxMenu()
menu.Append(ID_ABOUT, "&About", "More information about this program")
menu.AppendSeparator()
menu.Append(ID_EXIT, "E&xit", "Terminate the program")
menuBar = wxMenuBar()
menuBar.Append(menu, "&File");
self.SetMenuBar(menuBar)
self.Button1 = Custom_Button(self, parent, -1,
"D:/Documents/Python/Normal.bmp",
"D:/Documents/Python/Clicked.bmp",
"D:/Documents/Python/Over.bmp",
wx.Point(200,200), wx.Size(300,100))
EVT_MENU(self, ID_ABOUT, self.OnAbout)
EVT_MENU(self, ID_EXIT, self.TimeToQuit)
def OnAbout(self, event):
dlg = wxMessageDialog(self, "Testing the functions of custom "
"buttons using pyDev and wxPython",
"About", wxOK | wxICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def TimeToQuit(self, event):
self.Close(true)
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(NULL, -1, "wxPython | Buttons")
frame.Show(true)
self.SetTopWindow(frame)
return true
app = MyApp(0)
app.MainLoop()
/home/wallter/python/Custom Button overlay/src/Custom_Button.py:8: DeprecationWarning: The wxPython compatibility package is no longer automatically generated or actively maintained. Please switch to the wx package as soon as possible.
from wxPython.wx import *
Traceback (most recent call last):
File "/home/wallter/python/Custom Button overlay/src/Main.py", line 57, in <module>
app = MyApp(0)
File "/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py", line 7978, in __init__
self._BootstrapApp()
File "/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py", line 7552, in _BootstrapApp
return _core_.PyApp__BootstrapApp(*args, **kwargs)
File "/home/wallter/python/Custom Button overlay/src/Main.py", line 52, in OnInit
frame = MyFrame(NULL, -1, "wxPython | Buttons")
File "/home/wallter/python/Custom Button overlay/src/Main.py", line 32, in __init__
wx.Point(200,200), wx.Size(300,100))
TypeError: 'module' object is not callable
我尝试删除“wx.Point(200,200), wx.Size(300,100))
”只是为了让错误向上移动到上面一行。我声明了吗?帮助
使用此代码解决问题:here
答案 0 :(得分:2)
注释掉这一切,错误就消失了,一个窗口弹出:
self.Button1 = Custom_Button(self, parent, -1,
"D:/Documents/Python/Normal.bmp",
"D:/Documents/Python/Clicked.bmp",
"D:/Documents/Python/Over.bmp",
wx.Point(200,200), wx.Size(300,100))
所以到了这里......
好的发现了问题:
这是导入的Custom_Button。那只是导入模块。您希望在该模块中使用具有相同名称的类,而不是:
from Custom_Button import Custom_Button
这个问题已得到解决。然后,您可以继续下一个错误...
我很难立刻看到这个问题,因为你的编程风格与Python不同。与其他编程语言不同,Python在文档中定义了特定的编程风格。它在这里和那里有轻微的偏差,但总体上大多数优秀的Python开发人员都非常密切地关注它,因为它有意义并且有助于使定位问题更容易。
Google版本增加了更多内容,甚至更加严格。
所以在这种情况下,模块文件应改为命名为custom_button.py和main.py.该类应命名为CustomButton。
答案 1 :(得分:1)
在Python中导入模块时,请将其视为在C#
中将命名空间导入一级例如在C#中,当你说
时
import System.IO;
您可以指定
var file = new File("a.txt");
但是,如果你只是说import System;
,你必须说出来
var file = new IO.File("a.txt");
在Python中如果你做了等效的import System.IO
,你仍然需要说
file = IO.File("a.txt")
为了不需要在Python中开头,你需要说出来
from System.IO import File
或from System.IO import *
更像是C#版本。
能够指定特定类的优点是,当您拥有包含大量类的名称空间时,或者您要导入的2个模块中可能存在冲突的类名。在C#中,您需要为命名空间设置别名或指定全名,您只需在Python中导入所需的特定名称即可。