开发者

Importing classes in python

I have a little module that creates a window (program1). I'd like to import this into another python program of mine (program2).

How would I make it so 开发者_运维知识库I can make so program1 makes a window when referred to by program2?

The module I'd like to import(program1).

import Tkinter

class Class(Tkinter.Tk):

    def __init__(self, parent):

        Tkinter.Tk.__init__(self, parent)
        self.parent = parent

        self.Main()

    def Main(self):
        self.button= Tkinter.Button(self,text='hello')
        self.button.pack()



if __name__ == "__main__":
    app = Class(None)
    app.mainloop()

Edit

How do I make program 2 do the function call self.Main() that's in program1?

Also how do I go about transferring values across programs?

Say in program1 x = 'hello', how do I get the value of x in program2?


If you want to run the mainloop from program2, then:

import program1
app = program1.Class(None)
app.mainloop()

or, for less duplication:

# program1.py
...
def main():
  app = class(None)
  app.mainloop()

if __name__ == "__main__":
  main()

and then

# program2.py
import program1
program1.main()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜