Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中的不同类之间传递自声明变量_Python_Variables_Tkinter - Fatal编程技术网

如何在python中的不同类之间传递自声明变量

如何在python中的不同类之间传递自声明变量,python,variables,tkinter,Python,Variables,Tkinter,我正试图创建一个程序,跟踪自行车的间隔时间,并创建一个骑自行车时使用的路线图。我想在秒表计时的时候有一条红线跟踪这条线。但是,我无法在主应用程序类和秒表类之间传递画布 from Tkinter import * import random import time from itertools import product class App(): def __init__(self,master): menubar = Menu(master

我正试图创建一个程序,跟踪自行车的间隔时间,并创建一个骑自行车时使用的路线图。我想在秒表计时的时候有一条红线跟踪这条线。但是,我无法在主应用程序类和秒表类之间传递画布

from Tkinter import *
import random
import time
from itertools import product


    class App():

        def __init__(self,master):
            menubar = Menu(master)
            filemenu = Menu(menubar,tearoff=0)
            filemenu.add_command(label="Quit",command=root.destroy)
            filemenu.add_command(label="Interval",command=self.Interval)
            coursemenu = Menu(menubar,tearoff=0)
            coursemenu.add_command(label="New Random Course",command=self.regenerateTerrain)
            menubar.add_cascade(label="File",menu=filemenu)
            menubar.add_cascade(label="Course",menu=coursemenu)
            master.config(menu=menubar)        
            self.statusbar = Frame(master)
            self.statusbar.pack(side=BOTTOM, fill=X)
            self.infobar = Frame(master)
            self.infobar.pack(side=TOP,fill=X)
            self.course = Label(self.infobar, text="Welcome!")
            self.course.pack(side=LEFT)
            self.action = Label(self.infobar, text="")
            self.action.pack(side=RIGHT,fill=X)

            #Stopwatch

            stop = StopWatch(self.infobar)
            stop.pack(side=LEFT)

            #Stopwatch buttons
            self.button = Button(self.infobar, text="Start", command=stop.Start)
            self.button2 = Button(self.infobar, text="Stop", command=stop.Stop)
            self.button3 = Button(self.infobar, text="Reset", command=stop.Reset)
            self.button4 = Button(self.infobar, text="Quit", command=root.quit)

            self.button.pack(side=LEFT)
            self.button2.pack(side=LEFT)
            self.button3.pack(side=LEFT)
            self.button4.pack(side=LEFT)
            #Constants for program        
            #distance is in miles
            #height is in feet
            self.totalDistance = 25
            self.heightInterval = 10
            self.canvasHeight = 300
            self.canvasWidth = 500

            self.c=Canvas(root,width=self.canvasWidth,height=self.canvasHeight,background="white")

            self.c.pack(side=TOP,fill=BOTH,expand=YES)
            #Call regenerate an initial time, so that terrain gets generated on
            #initial creation
            self.regenerateTerrain()

        def buildTerrain(self,distance=25,topBound=0,
                         bottomBound=300,width=500):
            options=['up','down','flat','flat']
            y = (bottomBound-topBound)/2
            map = [y]
            changer =0
            for i in xrange(distance*10):
                direction = random.choice(options)
                options.pop()
                if direction=='up' and y>10:
                    options.append('up')
                    map.append(map[len(map)-1]-self.heightInterval)
                    changer=-self.heightInterval
                elif direction=='down' and y<bottomBound-10:
                    options.append('down')
                    map.append(map[len(map)-1]+self.heightInterval)
                    changer=self.heightInterval
                else:
                    options.append('flat')
                    map.append(map[len(map)-1])
                    changer=0
                y+=changer
            return map

        def regenerateTerrain(self,distance=25,topBound=0,bottomBound=300,width=500):
            self.c.delete(ALL)
            x = 0
            y = (bottomBound+topBound)/2
            self.build = self.buildTerrain()
            for i in xrange(1,len(self.build)-1):
                self.c.create_line(x,y,x+(self.canvasWidth/(self.totalDistance*10)),self.build[i],fill="black")
                x+=(self.canvasWidth/(self.totalDistance*10))
                y=self.build[i]
            self.c.create_oval(0,self.build[0]-1,4,self.build[0]-5,fill="red")

        def Interval(self):
            self.clock = StopWatch()
            top = Toplevel()
            top.title("Interval Mode")

            entLabelLow = Label(top)
            entLabelLow["text"] = "# of minutes at low interval: "
            entLabelHigh = Label(top)
            entLabelHigh["text"] = "# of minutes at high interval: "
            entLabelTotal = Label(top)
            entLabelTotal["text"] = "Total # of minutes"

            entWidgeTotal = Entry(top)
            entWidgeTotal["width"] = 5
            entWidgeLow = Entry(top)
            entWidgeLow["width"] = 5
            entWidgeHigh = Entry(top)
            entWidgeHigh["width"] = 5

            entLabelTotal.pack(side=LEFT)
            entWidgeTotal.pack(side=LEFT)
            entLabelLow.pack(side=LEFT)
            entWidgeLow.pack(side=LEFT)
            entLabelHigh.pack(side=LEFT)
            entWidgeHigh.pack(side=LEFT)

            self.linesDist = 0
            self.minutes = 0.0  
            self.timeatHL = 0
            self.timeatLL = 0
            self.currentPos = 0

            def drawGraph():
                if entWidgeLow.get().strip() == "" or entWidgeHigh.get().strip() == "":
                    print"Enter a value please"
                    pass
                    top.destroy()
                elif int(entWidgeLow.get().strip()) not in range(1,11) or int(entWidgeHigh.get().strip()) not in range(1,11):
                    print"Please enter a number between 1 and 10"
                    pass
                    top.destroy()

                else: #Get the values
                    self.LLength = int(entWidgeLow.get().strip())
                    self.HLength = int(entWidgeHigh.get().strip())
                    self.TLength = int(entWidgeTotal.get().strip())
                    top.destroy()
                while self.linesDist < self.canvasWidth - 50: #Create the vertical lines
                    self.c.create_line(10,195,10,205,fill="red")
                    self.linesDist += 50
                    self.intervalLength = self.TLength / 10.0 
                    self.minutes += float(self.intervalLength)
                    self.c.create_line((self.linesDist, 0, self.linesDist, 300), fill="gray")
                    self.c.create_text(self.linesDist, 290, text=str(self.minutes))
                #Now to draw the graph
                while self.currentPos < 500:
                    self.c.create_line(self.currentPos, 200, (((500/self.TLength)*self.LLength)+self.currentPos), 200)
                    self.currentPos += (float(self.LLength)/self.TLength) * 500
                    self.c.create_line(self.currentPos, 200, self.currentPos, 100)

                    self.c.create_line(self.currentPos, 100, (((500/self.TLength)*self.HLength)+self.currentPos), 100) 
                    self.currentPos += (float(self.HLength)/self.TLength) * 500
                    self.c.create_line(self.currentPos, 100, self.currentPos, 200)



            self.submit = Button(top, text="Submit", command = drawGraph)
            self.submit.pack(side=BOTTOM)

            self.c.delete(ALL)





    class StopWatch(Frame):
        def __init__(self, parent=None, **kw):
            """Creates the watch widget"""
            Frame.__init__(self, parent, kw)
            self._start = 0.0
            self._elapsed = 0.0
            self._running = 0
            self.timestr = StringVar()
            self.makeWidgets()

        def makeWidgets(self):
            """Make the label"""
            l = Label(self, textvariable=self.timestr)
            self._setTime(self._elapsed)
            l.pack(fill=X, expand=NO, padx=2, pady=2)
        def _update(self):
            """Update the label with the correct time"""
            self._elapsed=time.time() - self._start
            self._setTime(self._elapsed)
            self._timer = self.after(50, self._update)

            App.self.c.create_line(95,1,105,1)


        def _setTime(self, elap):
            """Set time string"""
            minutes = int(elap/60)
            seconds = int(elap - minutes*60.0)
            hundreths = int(((elap - minutes*60.0 - seconds)*100))
            self.timestr.set("%02d:%02d:%02d" % (minutes, seconds, hundreths))
        def Start(self):
            if not self._running:
                self._start = time.time() - self._elapsed
                self._update()
                self._running = 1


        def Stop(self):
            """To stop it, DUH"""
            if self._running:
                self.after_cancel(self._timer)
                self._elapsed = time.time() - self._start
                self._setTime(self._elapsed)
                self._running = 0
        def Reset(self):
            """Think about it"""
            if self._running:
                self.Stop()
            self._start = time.time()
            self._elapsed = 0.0
            self._setTime(self._elapsed)


    root=Tk()
    root.title("Bike Computer")
    myapp=App(root)
    root.mainloop()
从Tkinter导入*
随机输入
导入时间
来自itertools进口产品
类App():
定义初始(自我,主):
菜单栏=菜单(主菜单)
filemenu=Menu(menubar,tearoff=0)
filemenu.add_命令(label=“Quit”,command=root.destroy)
filemenu.add_命令(label=“Interval”,command=self.Interval)
coursemenu=菜单(菜单栏,tearoff=0)
coursemenu.add_命令(label=“New Random Course”,command=self.terrain)
menubar.add_级联(label=“File”,menu=filemenu)
菜单栏。添加\u级联(label=“Course”,menu=coursemenu)
master.config(menu=menubar)
self.statusbar=帧(主)
self.statusbar.pack(侧面=底部,填充=X)
self.infobar=帧(主)
self.infobar.pack(side=TOP,fill=X)
self.course=Label(self.infobar,text=“欢迎!”)
self.course.pack(侧面=左侧)
self.action=标签(self.infobar,text=”“)
self.action.pack(侧面=右侧,填充=X)
#秒表
停止=秒表(self.infobar)
停止。打包(侧面=左侧)
#秒表按钮
self.button=按钮(self.infobar,text=“Start”,command=stop.Start)
self.button2=按钮(self.infobar,text=“Stop”,command=Stop.Stop)
self.button3=按钮(self.infobar,text=“Reset”,command=stop.Reset)
self.button4=按钮(self.infobar,text=“Quit”,command=root.Quit)
自助按钮包(侧面=左侧)
自动按钮2.包装(侧面=左侧)
自动按钮3.包装(侧面=左侧)
自动按钮4.包装(侧面=左侧)
#程序常数
#距离以英里为单位
#身高以英尺为单位
self.totalDistance=25
自高度间隔=10
self.canvashheight=300
self.canvasWidth=500
self.c=Canvas(根,宽度=self.canvasWidth,高度=self.canvasHeight,background=“白色”)
self.c.pack(侧面=顶部,填充=两侧,展开=是)
#在初始时间调用REGENATE,以便生成地形
#初始创建
self.terrain()
def BUILD地形(自身,距离=25,上界=0,
底部边界=300,宽度=500):
选项=['up'、'down'、'flat'、'flat']
y=(下界上界)/2
map=[y]
转换器=0
对于X范围内的i(距离*10):
方向=随机。选择(选项)
options.pop()
如果方向==‘向上’且y>10:
options.append('up'))
map.append(map[len(map)-1]-self.heightInterval)
转换器=-self.heightInterval

elif direction=='down'和y变量声明为
self
的一部分,它们是实例变量-它们与类的特定实例相关联,而不是类本身-因此,为什么不能通过
App访问它们。
,因为这是在类中查找它们,而
c
实际上是
myapp
的成员

您需要做的是将应用程序的
self.c
传递给秒表构造函数(您需要向构造函数添加一个参数以接受它),然后将其本地存储在秒表中


self
是Python类中的一个特殊名称(从技术上讲,您可以随意调用它,但标准名称是
self
),它始终引用当前类实例。例如,下面的代码:

class A:
    def __init__(self, foo):
        self.bar = foo

    def echo(self):
        print self.bar

one = A(1)

one.echo() # prints '1'

two = A(2)

two.echo() # prints '2'
one.echo() # still prints '1'

如果一个类的所有实例都共享了
self
,则上述操作将不起作用。

声明为
self
一部分的变量是实例变量-它们与类的特定实例相关联,而不是类本身-因此,您无法通过
应用程序访问它们。
,因为这是在类中查找它们,当
c
实际上是
myapp
的成员时

您需要做的是将应用程序的
self.c
传递给秒表构造函数(您需要向构造函数添加一个参数以接受它),然后将其本地存储在秒表中


self
是Python类中的一个特殊名称(从技术上讲,您可以随意调用它,但标准名称是
self
),它始终引用当前类实例。例如,下面的代码:

class A:
    def __init__(self, foo):
        self.bar = foo

    def echo(self):
        print self.bar

one = A(1)

one.echo() # prints '1'

two = A(2)

two.echo() # prints '2'
one.echo() # still prints '1'

如果一个类的所有实例都共享了
self
,那么上面的方法就行不通了。

我只会传递所需的内容,即只传递对画布的引用,而不是对整个应用程序对象的引用。我该怎么做呢?我对Tkinter有点陌生。我只会传递所需的内容,即只传递对画布的引用,而不是对整个应用程序对象的引用。我该怎么做呢?我对特金特有点陌生。