使用tkinter作为GUI并使用它来运行程序

时间:2016-04-30 05:49:13

标签: python user-interface tkinter tkinter-canvas

我最近开始使用python,现在我正在尝试使用tkinter创建一个gui。我正在使用最新版本的PyCharm社区版。我需要让它看起来像在PyCharm中正常使用,并使其看起来更具吸引力,并允许输入文本和点击。大多数教程都已过时,不再适用于PyCharm。我花了10分钟为tkinter本身找到了正确的import语句。我需要创建一个文本框,我可以根据下面的代码输入所需的内容。就像它说按下进入一圈时我需要按回车键使其在文本框中单圈。有人可以只为其中一个raw_input演示,然后我可以将其应用于其余部分吗?提前致谢。

以下是代码:

keyboard Target ->Capabilities -> Enabling Inapp

1 个答案:

答案 0 :(得分: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()