Python程序只能从IDLE运行

时间:2019-02-21 00:17:08

标签: python

这是我第一次在python上运行,我绝对不是代码编写者。我有一个项目,我需要编写一个GUI来命令arduino,在经过大量谷歌搜索和拼凑代码之后,我使该程序在从IDLE中运行时可以完美运行。当我从Windows(双击它)或从Linux(通过命令行python3filler.py)启动它时,它会像出现错误一样打开和关闭。我可以通过相同的方法启动其他python程序而不会出现问题。在从IDLE中启动它以使硬件运行不是问题时,我还是不能放弃,因为我希望为以后的项目获得更多关于python的知识。任何帮助将不胜感激。

import tkinter as tk
import serial as s
import time as t
from tkinter import *



class Action:
    def __init__(self):


        def on():
            ser.write(b'7')

        def exit():
            ser.close() # close serial port
            quit()

        self.window = Tk()
        self.window.title("Moore Speciality Brewing")
        self.window.geometry('640x480')
        self.lbl = Label(self.window, text="Can Filler",fg='black',font=(None, 15))
        self.lbl.place(relx=0.24, rely=0.10, height=50, width=350)
        bo = Button(self.window, text="Fill Can", width=10 ,bg='red' ,command=on)
        bo.place(relx=0.34, rely=0.30, height=40, width=200)
        ext = Button(self.window, text="Exit", width=10, bg='white', command=exit)
        ext.place(relx=0.34, rely=0.50, height=40, width=200)

class Prompt(tk.Tk):
    def __init__(self):
        global comm
        comm = None               
        tk.Tk.__init__(self)
        self.geometry('640x480')
        self.label = tk.Label(self, text="Comm Port",fg='black',font=(None, 15))
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Get", command=self.on_button)

        self.entry.place(relx=.5, rely=.5,anchor="center" )
        self.label.place(relx=.5, rely=.44,anchor="center")
        self.button.place(relx=.5, rely=.56,anchor="center")

    def on_button(self):
        comm = self.entry.get()
        global ser
        ser = s.Serial(comm, 9600, timeout=0)   # check your com port
        t.sleep(2)
        Action()
        self.destroy()

Prompt()

2 个答案:

答案 0 :(得分:0)

您已经在代码中导入了时间。 只需在代码末尾使用t.sleep(60),使cli等待您查看是否有错误,然后进行调试即可。

末尾Prompt()

不正确。使用这样的东西:

myPrompt = Prompt()
myPrompt.mainloop() 

这部分实际上称为tkinter gui。

答案 1 :(得分:0)

您需要调用mainloop。
删除对head(dataseta) villageID lat lon 1 10.193657 39.34577 2 9.028250 38.75095 3 9.332589 42.13704 4 9.318443 42.11340 5 4.485986 40.03652 6 10.274364 34.93280 head(datasetb) lon lat tmax_1 1 0.125 59.875 72.2233 2 0.375 59.875 72.2275 3 0.625 59.875 72.2318 4 0.875 59.875 72.2361 5 1.125 59.875 72.2421 6 1.375 59.875 72.2531 (最后一行)的调用,并用类似下面的内容代替(在脚本底部):

Prompt()

有关api

的更多信息