简体   繁体   English

Python 程序只能从 IDLE 运行

[英]Python program will only run from IDLE

This is my first run at python and i am by no means a code writer.这是我第一次在 python 上运行,我绝不是代码编写者。 I have a project that I needed to write a GUI to command the arduino and after much googling and piecing together code I made this program which works perfectly when ran from IDLE.我有一个项目,我需要编写一个 GUI 来命令 arduino,经过大量的谷歌搜索和拼凑代码后,我制作了这个程序,该程序在从 IDLE 运行时运行良好。 When I launch it from windows (double click it) or from linux (via command line python3 filler.py) it opens and closes like there is an error.当我从 Windows(双击它)或从 linux(通过命令行 python3filler.py)启动它时,它会像出现错误一样打开和关闭。 I can launch other python programs by same methods with no problems.我可以通过相同的方法启动其他 python 程序,没有问题。 Where as its not an issue to launch it out of IDLE to make the hardware operate I just cant give up as I would like to get more knowledgeable with python for future projects.因为从 IDLE 启动它以使硬件运行不是问题,所以我不能放弃,因为我想在未来的项目中更多地了解 Python。 Any help would be greatly appreciated.任何帮助将不胜感激。

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()

you have already imported time in your code.您已经在代码中导入了时间。 just use t.sleep(60) at the end of your code to make the cli wait for you to see if there is an error and let you debug.只需在代码末尾使用t.sleep(60)即可让 cli 等待您查看是否有错误并让您进行调试。

at the end Prompt() is not correct.最后Prompt()不正确。 use something like this:使用这样的东西:

myPrompt = Prompt()
myPrompt.mainloop() 

this part actualy calls the tkinter gui.这部分实际上称为 tkinter gui。

You need to call the mainloop.您需要调用主循环。
Remove the call to Prompt() (last line) and substitute it with something like this (at the bottom of the script):删除对Prompt()的调用(最后一行)并将其替换为如下内容(在脚本底部):

if __name__ == "__main__":
    prm = Prompt()
    prm.mainloop()

Here more on tkinter mainloop这里有更多关于tkinter 主循环的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM