2016-04-30 64 views
0

我最近開始使用python,現在我正試圖用tkinter做出一個gui。我正在使用最新版本的PyCharm社區版。我需要讓它看起來像它在PyCharm中的正常工作,並使它看起來更具吸引力,並允許輸入文本和點擊。大多數教程已過時,不再與PyCharm一起使用。我花了10分鐘才找到tkinter本身的正確導入聲明。我需要創建一個文本框,以便我可以按照以下代碼的要求輸入內容。就像當它說按下輸入圈,我需要按下輸入,使其在文本框中圈。有人可以證明對於其中一個raw_input,然後我可以將其應用於其餘的?提前致謝。使用tkinter作爲GUI並使用它來運行程序

下面是代碼:

import time 
laps=[0] 

def lap(): 
    lapping = raw_input("lap? (press enter to lap) to stop type something first then press enter") 
    if lapping == "": 
     end() 

    while lapping == "": 
      break 
      lap() 

def end(): 
    end = time.time() 
    elapsed = end - start 
    laps[0]+=1 
    print "%s seconds" % (elapsed) 
    print "%s lap(s)" % (laps) 
    lap() 

starting = raw_input("start? (press enter to start)") 
if starting == "": 
    start = time.time() 
    ending = raw_input("end? (press enter to end)") 
    if ending == "": 
     end() 
+0

你忘了其實包括問題 – tobspr

+0

代碼看起來非常縮進甚至在鏈路(函數的內容'end')。我試圖糾正它,並直接將其添加到您的問題中,請檢查它,並在萬一您希望的情況下,刪除鏈接的最後一句。 – quapka

+0

@quapka非常感謝,我直接從PyCharm複製代碼。 –

回答

0

免責聲明:我用的Tkinter只有一次之前沒有了經驗。不過,儘管我可以將這些代碼放在一起。儘管我對很多部分都不滿意,但它最終還是可行的。希望它能爲你服務,因爲我的出發點。但說實話,搜索谷歌會給你更好,更清晰的例子。該代碼也在我的github上。

import tkinter as tk 
import time 


class App(tk.Tk): 

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     # boolean for checking, whether the stopwatch currently run 
     self.running = False 
     # the starting time 
     self.startTime = 0 
     # StringVar so the text inside self.label is updated on change 
     self.timeLabelText = tk.StringVar() 
     self.label = tk.Label(textvariable=self.timeLabelText) 
     # pack it in the frame 
     self.label.pack() 
     # similar code for button, added self._runButton when the button is clicked 
     self.runButtonText = tk.StringVar() 
     self.runButtonText.set("Run") 
     self.runButton = tk.Button(textvariable=self.runButtonText, 
            command=self.__runButton) 
     self.runButton.pack() 
     # array to hold the laps 
     self.laps = [] 
     self.lapButton = tk.Button(text="Lap", 
            command=self.__lapButton) 
     self.lapButton.pack() 
     # bind Return with __lapButton function 
     self.bind("<Return>", self.__lapButton) 

    def __timelabel(self): 
     # if running change the time each second 
     if self.running: 
      seconds = time.time() - self.startTime 
      self.timeLabelText.set("%d:%02d:%02d" % seconds_to_time(seconds)) 
      self.after(1000, self.__timelabel) 

    def __runButton(self): 
     # if "was" running, set running to false and adjust label 
     if self.running: 
      self.running = False 
      self.runButtonText.set("Run") 
     # if "was not" running 
     else: 
      # get the time 
      self.startTime = time.time() 
      # change running to True 
      self.running = True 
      # start stopwatch immediately 
      self.after(0, self.__timelabel) 
      # change the text on the runButton 
      self.runButtonText.set("Stop") 
      # clear each lap from previous run 
      for lap in self.laps: 
       lap.pack_forget() 
      # and empty it, maybe use while loop and pop 
      self.laps = [] 

    def __lapButton(self, *args): 

     if self.running: 
      # get the time as str "HH:MM:SS" 
      t = self.timeLabelText.get() 
      t = time_to_seconds(t) 
      # if there are previous laps 
      if self.laps: 
       # get their sum 
       elapsed = sum([time_to_seconds(self.laps[x].cget("text")) for x in range(len(self.laps))]) 
       # substract that 
       t  = t - elapsed 
      t = seconds_to_time(t) 
      t = ":".join([str(x) for x in t]) 
      # add new label with text 
      self.laps.append(tk.Label(text=t)) 
      # pack all the label 
      for lap in self.laps: 
       lap.pack() 

# methods for handling time 
# probably it would be better to use some precoded method from 
# module time and datetime 
def time_to_seconds(time): 

    h, m, s = [int(x) for x in time.split(':')] 
    return h*3600 + m*60 + s 

def seconds_to_time(seconds): 

    m, s = divmod(seconds, 60) 
    h, m = divmod(m, 60) 
    return h, m, s 

if __name__ == '__main__': 

    App().mainloop() 
+0

好的謝謝,我發現_tkinter和Tkinter是不同的。我想這就是爲什麼我搞砸了。非常感謝你的幫助。 –

+0

另外,有沒有辦法打印他們已經完成的圈數?我環顧了你所做的事情,並且我最瞭解你所做的一切。它只是當你展示時間時,是否有辦法顯示已經完成的圈? –

+0

@architsrivastava是的,它可以。可能有更好的方法,但我認爲可以只改變__lapButton函數。我會補充說,以後:) – quapka

相關問題