运行shell命令时,python脚本只在停止之前运行一半

时间:2017-08-02 20:16:56

标签: python python-3.x sh raspbian raspberry-pi3

我创建了一个python 3脚本,使用easygui获取各种数字的用户输入,然后根据这些数字计算时间,然后从tkinter创建一个计时器GUI。那个脚本运行得很好。但是,我创建了一个sh文件,基本上使脚本可以从Raspberry Pi 3的桌面上执行,但它只运行脚本的easygui部分(用户输入数字)。输入最后一个数字后,对话框将按预期关闭,但计时器窗口永远不会打开。

我的shell文件如下:

#!/bin/bash

cd /home/pi/Desktop/Python/easygui-0.97/easygui
python potential.py

我的其余代码(名为“potential”的python脚本)

from partsperhour import hours_to_completion
from partsperhour import part_number
from partsperhour import cycle_time
from partsperhour import cavities
from partsperhour import work_order_amount


import math
hours_to_completion
dec,int_ = math.modf(hours_to_completion)
dec_=dec*60


str1=str(int(int_))
str2=":"
str3=str(int(dec_))
str4="00"
str5="0"



import tkinter as tk
from tkinter import ttk
from tkinter import *

def update_timeText():
    if (state):
        global timer
        timer[2] -= 1
        if (timer[2] <= 0 and timer[1] <=0):
             timer[2] = 59
             timer[1] -= 1
             timer[1] = 59
             timer[0] -= 1
        if (timer[2] <= 0 and timer[0] !=0 and timer[1] >0):
             timer[1] -= 1
             timer[2] = 59
        if (timer[2] <= 0 and timer[0] <=0 and timer[1] >0):
             timer[1] -= 1
             timer[2] = 59

        if (timer[1] <= 0 and timer[0] <= 0):
             timer[2] = 59    

        if (timer[0] !=0 and timer[1] != 0 and timer[2] == 0):
             timer[2] =59
             timer[1] -=1
             timer[2] -=1
        if (timer[0] ==0 and timer[1] ==0):
            timer[2] -=1








        timeString = pattern.format(timer[0], timer[1], timer[2])
        timeText.configure(text=timeString)
    root.after(1000, update_timeText)





def start():
    global state
    state = True

def pause():
    global state
    state = False

def reset():
    global timer
    timer = [int(int_), int(dec_), 0] #change values here
    timeText.configure(str1+str2+str5+str3+str2+str4) #change values here

def exist():
    root.destroy()

state = False



partstr=str(part_number)
partstr2="Hours until "
partstr3=" is finished"
root = tk.Tk()
root.wm_title(partstr2+partstr+partstr3)

timer = [int(int_), int(dec_), 0] #change values here for reset
pattern = '{0:02d}:{1:02d}:{2:02d}'

timeText = ttk.Label(root, text=str1+str2+str5+str3+str2+str4, font=
("Helvetica", 100)) #change values here
timeText.pack()

startButton = tk.Button(root, text='Start', command=start, height = 2, width 
= 10,bg = "green",font=('Helvetica MS',15),borderwidth = 4,relief=RIDGE)
startButton.pack(side="left")

pauseButton = tk.Button(root, text='Pause', command=pause, height = 2, width 
= 10,bg = "yellow",font=('Helvetica MS',15),borderwidth = 4,relief=RIDGE)
pauseButton.pack(side="left")

resetButton = tk.Button(root, text='Reset', command=reset, height = 2, width 
= 10,bg = "red",font=('Helvetica MS',15),borderwidth = 4,relief=RIDGE)
resetButton.pack(side="right")

quitButton = tk.Button(root, text='Exit', command=exist, height = 2, width = 
10,bg = "red",font=('Helvetica MS',15),borderwidth = 4,relief=RIDGE)
quitButton.pack(side="right")

w = Label(root, text="Part No. "+partstr, font = "Times 16 bold")
w.pack()


w2 = Label(root, text="Cycle Time: "+cycle_time+" sec", font = "Times 16 
bold")
w2.pack()


w3 = Label(root, text="Number of Cavities: "+cavities, font = "Times 16 
bold")
w3.pack()


w4 = Label(root, text="Total work order amount: "+work_order_amount, font = 
"Times 16 bold")
w4.pack()





update_timeText()
root.mainloop()

可以看到它所提供的partperhour值:

import tkinter
from tkinter import ttk
def main():
    root = tkinter.Tk()
    numpad = NumPad(root)
    root.mainloop()




import easygui as eg

message1 = "Enter the part number"
part_number = eg.enterbox(message1, "Part Number",)  

message2 = "Enter the cycle time in seconds"
cycle_time = eg.enterbox(message2, "Cycle time",)

message3 = "Enter the number of cavities"
cavities = eg.enterbox(message3, "Cavities",) #User inputs the number of 
cavities

message4 = "Enter the total number required from Work Order"
work_order_amount = eg.enterbox(message4, "Work Order Amount",) #total 
number of parts needed

parts_per_hour = (3600/float(cycle_time))*int(cavities) #calculates how many 
parts will be produced in 1 hour

hours_to_completion = int(work_order_amount)/int(parts_per_hour) #calculates 
how many hours in total it will take

eg.msgbox(hours_to_completion, "Hours until complete")

因此,基本上当我运行sh文件时,它将执行来自partperhour代码的所有操作,但在弹出最后一个框并且用户按下“OK”后,计时器根本不会显示。值得一提的是,如果我通过IDLE 3运行该模块,一切都按预期工作。只有在我尝试在桌面上使用shell文件时才会这样做。

我不是最有经验的python,但我觉得我已经相处得很远了。如果有人有一些很棒的输入!

谢谢!

0 个答案:

没有答案