2015-01-13 3 views
-4

알려진 거리 (100m) 이상으로 자동차 속도를 계산하는 프로그램을 만들어야합니다. 사용자가 누를 때 시작하는 프로그램이 필요합니다 을 입력하십시오 (자동차가 모니터링되는 구간으로 들어가면 시뮬레이트해야 함). 사용자가 을 누를 때 중지 타이밍을 다시 입력 한 다음 자동차의 평균 속도를 계산하십시오.어떻게 파이썬에서 버튼을 입력으로 사용합니까?

+0

을 당신은 현실 세계 자동차 추적을 의미 :

root = Tk() 행 다음에 추가, 메인 윈도우를 크게하려면? 이미지 처리를 찾아야합니다. –

+0

SO는 코드 작성 서비스가 아니므로 많은 작업을 직접 처리해야합니다. 타이밍에 대해서는'time' 모듈을, 버튼/GUI에 대해서는'tkinter '를 보도록 권합니다. 그 (것)들에 읽 거든 당신은 너 자신을 만드는 것을 시도한 프로그램에 대한 문제가있는 경우에, 그 후에 물으십시오. – Scironic

+0

문제에 대해보다 정확하게 설명하십시오. 사용자 입력을받는 방법을 알고 있습니까? 시간을 얻는 방법? 거리와 시간에 주어진 속도를 계산하는 방법? – jonrsharpe

답변

1

사용자가 Enter 키를 누를 때 시작되는 타이밍 (자동차가 감시되는 구간으로 들어가는 경우 시뮬레이트해야 함), 사용자가 다시 Enter 키를 누를 때의 타이밍을 중지 한 다음 자동차의 평균 속도를 계산합니다.

CLI 버전 :

#!/usr/bin/env python3 
from time import monotonic as timer 

input("Press Enter to start timer") # wait for the first Enter from the user 
start = timer() 
input("Press Enter to stop timer") 
elapsed = timer() - start 
print("Speed {speed:f} m/s".format(speed=100/elapsed)) 

는 GUI 스톱워치를 만들려면, 당신은 tkinter을 사용할 수

#!/usr/bin/env python3 
from time import monotonic as timer 
from tkinter import font, Tk, ttk 

distance = 100 # meters 
START, STOP = 'start', 'stop' 

class Stopwatch(object): 
    def __init__(self, parent): 
     self.parent = parent 
     # create a button, run `.toggle_button` if it is pressed 
     self.button = ttk.Button(parent,text=START, command=self.toggle_button) 
     self.button.grid(sticky='nwse') # lays out the button inside the parent 

    def toggle_button(self, event=None): 
     if self.button is None: # close parent window 
      self.parent.destroy() 
     elif self.button['text'] == START: # start timer 
      self.start = timer() 
      self.button['text'] = STOP 
     elif self.button['text'] == STOP: # stop timer, show result 
      elapsed = timer() - self.start 
      self.button.destroy() 
      self.button = None 
      text = "Speed {:.2f} m/s".format(distance/elapsed) 
      ttk.Label(self.parent, text=text, anchor='center').grid() 

root = Tk() # create root window 
root.protocol("WM_DELETE_WINDOW", root.destroy) # handle root window deletion 
root.bind('<Return>', Stopwatch(root).toggle_button) # handle <Enter> key 
root.mainloop() 

둘 다 눌러 를 입력하고 .toggle_button() 메소드를 호출 버튼을 클릭 할 수 있습니다.

root.title('Stopwatch') # set window title 
root.geometry('400x300') # set window size 
root.columnconfigure(0, weight=1) # children widgets may fill the whole space 
root.rowconfigure(0, weight=1) 
font.nametofont('TkDefaultFont').configure(size=25) # change default font size 
관련 문제