如何将Tkinter GUI和乌龟图形结合起来?

时间:2018-12-01 22:47:12

标签: python-3.x tkinter turtle-graphics

我仍然是python的初学者,但是我试图使用tkinter GUI界面在提交时将数据绘制到龟图形窗口。我在合并它们时遇到了麻烦,因为我从未做过。目前它们是分开工作的,但是我希望输出数据在turtle中创建条形图。 (并在重新提交后向上移动以创建一个新的)。我不知道是否有更好的方法可以做到这一点,但我更愿意使用乌龟。

这是我程序的GUI部分

from tkinter import *
from tkinter.messagebox import showerror
from turtle import RawTurtle, TurtleScreen, ScrolledCanvas

class calculator(Frame):
#Look up dictionaries for friction coefficients
    tireDict = {'Normal':0.13, 'Performance':0.14, 'Winter':0.09, 'Racing':0.16, 'Wagon Wheel':0.03}
    roadDict = {'Dry':0.13, 'Wet':0.07, 'Ice':0.02, 'Oil':0.01, 'Absolute Friction':1.0}


    def __init__(self):
        Frame.__init__(self)
        self.master.title("Stopping Distance Calculator")
        self.grid(sticky=W+E+N+S)
        self.master.geometry("330x240")
        self.master.rowconfigure(0, weight=1)
        self.master.columnconfigure(0, weight=1)

        for row in range(4):
            self.rowconfigure(row, weight=1)
        for column in range(6):
            self.columnconfigure(column, weight=1)

        #set up input pane
        self.inputsPane = Frame(self)
        self.inputsPane.grid(row=3, column = 1)

        #set up vehicle speed input
        self.speedLabel = Label(self.inputsPane, text="Speed in mph")
        self.speedLabel.grid(row=0, column=0, sticky=W)
        self.speedVar= DoubleVar()
        self.speedEntry = Entry(self.inputsPane, justify="right", textvariable = self.speedVar)
        self.speedEntry.grid(row=0, column=1, padx=13, pady=10)

        #set up type of tire input
        self.tireLabel = Label(self.inputsPane, text="Type of tire")
        self.tireLabel.grid(row=1, column=0, sticky=W)
        tireList = ["Normal", "Performance", "Winter", "Racing", "Wagon Wheel"]
        self.tireVar=StringVar()
        self.tireVar.set("Normal")
        self.tireMenu = OptionMenu(self.inputsPane, self.tireVar, *tireList)
        self.tireMenu.grid(row=1, column=1, columnspan=3, padx=13, pady=10, ipadx=2)

        #set up type of road surface input
        self.roadLabel = Label(self.inputsPane, text="Type of road surface")
        self.roadLabel.grid(row=2, column=0, sticky=W)
        roadList = ["Dry", "Wet", "Ice", "Oil", "Absolute Friction"]
        self.roadVar=StringVar()
        self.roadVar.set("Dry")
        self.roadMenu = OptionMenu(self.inputsPane, self.roadVar, *roadList)
        self.roadMenu.grid(row=2, column=1, padx=13, pady=10, ipadx=15)

        #set up brake power input
        self.brakeLabel = Label(self.inputsPane, text="Set the brake power")
        self.brakeLabel.grid(row=3, column=0, sticky=W)
        self.brakeVar=IntVar()
        self.brakeSlider = Scale(self.inputsPane, variable=self.brakeVar, from_=1, to=100, tickinterval=100, orient=HORIZONTAL)
        self.brakeSlider.grid(row=3, column=1, ipadx=10)

        #set up button pane
        self.buttonPane = Frame(self)
        self.buttonPane.grid(row=4, column = 1)

        #set up buttons
        self.submit = Button(self.buttonPane, text="Submit", command = self._submit)
        self.submit.grid(row=4, column=0, padx=10, pady=5)
        self.reset = Button(self.buttonPane, text="Reset", command=self._reset)
        self.reset.grid(row=4, column=1, padx=10, pady=5, ipadx=5)
        self.quit = Button(self.buttonPane, text="Quit", command=self._quit)
        self.quit.grid(row=4, column=2, padx=10, pady=5, ipadx=7)


    #Submit button function, testing inputs/outputs currently
    def _submit(self):
        print (self._speedConvert(self.speedVar))
        print (self._lookupTire(self.tireVar))
        print (self._lookupRoad(self.roadVar))
        print (self.brakeVar.get())
        print (self._frictionC())

        print (str(self._stopDistance()) + " feet")

    #Reset button function
    def _reset(self):
        self.weightVar.set(0.0)
        self.speedVar.set(0.0)
        self.tireVar.set("Normal")
        self.roadVar.set("Dry")
        self.brakeVar.set(1)

    #Quit button function
    def _quit(self):
        self.master.destroy()

    #converts mph to m/s
    def _speedConvert(self, x):
        try:
            return self.speedVar.get()/2.237
        except:
            showerror(title="Error", message="Error: Speed must be number")

    #Looks up tire FrC from dictionary
    def _lookupTire(self, x):
        return calculator.tireDict[self.tireVar.get()]

    #Looks up road FrC from dictionary
    def _lookupRoad(self, x):
        return calculator.roadDict[self.roadVar.get()]

    #Calulates friction coefficent
    def _frictionC(self):
        Ft = self._lookupTire(self.tireVar.get())
        Fr = self._lookupRoad(self.roadVar.get())
        Bp = float(self.brakeVar.get())
        return (Ft + Fr)*(Bp/10)

    #Calculates stop distance in feet    
    def _stopDistance(self):
        v = self._speedConvert(self.speedVar.get())
        Fc = self._frictionC()
        g = float(9.80)
        Td = (self.speedVar.get())/3.33
        Tm = (v**2) / (Fc * g)*2
        m = (Tm + Td)
        return round((m*3.28084), 2)

#Main Function
def main():
    calculator().mainloop()   

main()

这是我到目前为止的乌龟部分

t = RawTurtle()
s = TurtleScreen()

def drawGraph(t):
    s.setup(1000, 500)
    s.title("Distance Graph")
    t.screen.bgcolor("white")
    t.width(3)
    t.up()
    t.pencolor("black")
    t.goto(-460, -210)
    t.down()
    t.goto(460, -210)
    t.up()
    t.goto(-460, -210)
    t.down()
    t.goto(-460, 220)
    t.up()
    t.goto(-460, -210)

    for count in range(-460, 465, 40):
        t.goto(count, -210)
        t.down()
        t.goto(count, -215)
        t.up()
        t.goto(count, -210)

    t.goto(-20, -243)
    t.write("Distance in ft")

    t.goto(-460, -230)
    for count in range(0, 930, 40):
        t.write(count)
        t.goto(-425.5+count, -230)

    t.goto(-480, -200)
    x = 1
    for count in range(0, 460, 45):
        t.write(x, move="false", align="center")
        x += 1
        t.goto(-480, -155+count)

    #testing bar graph, will be controlled by submit output in new method when combined
    t.goto(-457, -195)
    t.down()
    t.width(7)
    t.pencolor("red")
    t.goto(0, -195)
    t.pencolor("black")
    t.write("500")
    t.up()
    t.goto(-457, -150)
    t.down()
    t.pencolor("blue")
    t.goto(300, -150)


def main():
    drawGraph(t)

main()

非常感谢任何帮助或见识

0 个答案:

没有答案