2017-09-12 23 views
2

我試圖從Tkinter的執行蟒蛇3文件,當我點擊一個按鈕如何Tkinter的執行Python 3的程序一個鍵

Tkinter的代碼

import tkinter as tk 
import subprocess as sub 

WINDOW_SIZE = "600x400" 

root = tk.Tk() 
root.geometry(WINDOW_SIZE) 

tk.Button(root, text="Create Motion!", command=lambda: sub.call('home/pi/motion1.py')).pack() 

但收到錯誤時我運行程序

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__ 
    return self.func(*args) 
    File "/home/pi/AnimationGUI.py", line 11, in <lambda> 
    tk.Button(root, text="Create Motion!", command=lambda: sub.call('motion1.py')).pack() 
    File "/usr/lib/python3.5/subprocess.py", line 247, in call 
    with Popen(*popenargs, **kwargs) as p: 
    File "/usr/lib/python3.5/subprocess.py", line 676, in __init__ 
    restore_signals, start_new_session) 
    File "/usr/lib/python3.5/subprocess.py", line 1282, in _execute_child 
    raise child_exception_type(errno_num, err_msg) 
FileNotFoundError: [Errno 2] No such file or directory: 'motion1.py' 
+0

嗨,感謝您的迴應,我以爲是我給了我做錯了什麼的絕對路徑? – Coyney22

+0

也許你在「home/pi/motion1.py」 –

+0

前需要一個「/」,因爲它解決了那個部分,但是現在獲得[Errno 13]權限被拒絕了? – Coyney22

回答

0

你這樣做是錯的。做到這一點的正確方法是讓「motion1.py」中的某個函數做一些事情。假設你稱這個函數爲「main」(很常見)。那麼你的代碼是:

import tkinter as tk 
import motion1 

WINDOW_SIZE = "600x400" 

root = tk.Tk() 
root.geometry(WINDOW_SIZE) 

btn = tk.Button(root, text="Create Motion!", command=motion1.main) 
btn.pack() 

假設你的代碼和「motion1.py」在同一個文件夾中。

+0

但是,感謝上面的代碼,它沒有繪製窗口按下按鈕,它調用motion1.py並啓動相機? – Coyney22

+0

以下錯誤消息btn = tk.Button(root,text =「Create Motion!」,command = motion1.main) AttributeError:模塊'motion1'沒有屬性'主要 – Coyney22

+0

@ Coyney22您需要將所有代碼放入首先命名爲「main」。 – Novel