小编sen*_*rle的帖子

从类内部绑定小部件

我在这个主题上看到的每个例子都显示一个Button被绑定到一个命令,除了Button小部件是在一个类之外创建的:

例如:

from Tkinter import *

root = Tk()

def callback(event):
    print "clicked at", event.x, event.y 

frame = Frame(root, width=100, height=100) 
frame.bind("<Button-1>", callback) 
frame.pack()

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

现在没问题,除了我在尝试执行以下操作时遇到错误:

from Tkinter import *
class App():
    def __init__(self,parent):
        o = Button(root, text = 'Open', command = openFile)
        o.pack()
    def openFile(self):
        print 'foo'


root = Tk()
app = App(root)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

用"command = self.openFile()"或"command = openFile()"替换"command = openFile"也不起作用.

如何将函数绑定到我的类中的Button?

python tkinter

0
推荐指数
1
解决办法
900
查看次数

Web2Py - 上传文件并将内容读取为Zip文件

我正在尝试从Web2Py表单上传一个zip文件,然后阅读内容:

form = FORM(TABLE(
           TR(TD('Upload File:', INPUT(_type='file', 
                                       _name='myfile', 
                                       id='myfile', 
                                       requires=IS_NOT_EMPTY()))), 
           TR(TD(INPUT(_type='submit',_value='Submit')))
       ))

if form.accepts(request.vars):  
    data=StringIO.StringIO(request.vars.myfile)  
    import zipfile  
    zfile=zipfile.Zipfile(data)
Run Code Online (Sandbox Code Playgroud)

由于某些原因,虽然上传的文件是zip文件,但此代码确实有效并且抱怨文件不是zip文件.

我是新来的Web2Py.如何data表示为zip文件?

python web2py zipfile

0
推荐指数
1
解决办法
4694
查看次数

Python:构建树

我正在努力建造一棵树.我乞求下一段代码:

>>> class tree:
    def __init__(self, charge, left=None, right=None):
        self.charge = charge
        self.left = left
        self.right = right

>>> class tree:
    def __str__(self):
        return str(self.charge)
Run Code Online (Sandbox Code Playgroud)

写完之后我写了下一篇

>>> left = tree(2)
Run Code Online (Sandbox Code Playgroud)

我这样写是因为我应该按照我使用的手册进行教学.但是我收到此错误:

Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
left = tree(2)
TypeError: this constructor takes no arguments
Run Code Online (Sandbox Code Playgroud)

如何使用从下到上的开始代码构建一棵树?顺便说一句,我的python版本是2.7.2.非常感谢你的帮助.

python binary-tree

0
推荐指数
1
解决办法
580
查看次数

计数功能的运行时复杂性

def findTarget(myList, target):

    count = 0

    for item in myList:

         if (target == item):

              count = count + 1

    return count
Run Code Online (Sandbox Code Playgroud)

有人告诉我这是0(log)n虽然我相信这是0(1)?有人可以确认还是否认?

python complexity-theory list

0
推荐指数
1
解决办法
1604
查看次数