2016-11-24 20 views
0

私は2つのpythonファイルを持っています。 1つはhelloworld.py、2つ目はmain.pyです。 main.pyにはbuttonがあります。そのボタンをクリックすると、helloworld.pyの結果をテキストボックスに出力します。Python tkinter GUIでCLI結果を表示

helloworld.py

印刷( "こんにちは世界")

ので、私が代わりにos.systemの使用subprocess.check_output main.pyテキストボックスにハロー

from tkinter import * 
import os 
root= Tk() 
root.title("My First GUI") 
root.geometry("800x200") 
frame1=Frame(root) 
frame1.grid() 

def helloCallBack(): 
    result = os.system('python helloworld.py') 
    if result==0: 
     print("OK") 
     text1.insert(END,result)   
    else: 
     print("File Not Found") 

label1 = Label(frame1, text = "Here is a label!") 
label1.grid() 

button1 = Button(frame1,text="Click Here" , foreground="blue",command= helloCallBack) 
button1.grid() 

text1 = Text(frame1, width = 35, height = 5,borderwidth=2) 
text1.grid() 

radiobutton1 = Radiobutton(frame1, text= "C Programming", value=0) 
radiobutton1.grid() 
radiobutton2 =Radiobutton(frame1, text= "Python Programming") 
radiobutton2.grid() 
root.mainloop() 

答えて

1

を世界の文字列を印刷したい:

from tkinter import * 
from subprocess import check_output, CalledProcessError 

root = Tk() 

text1 = Text(root) 
text1.pack() 

def command(): 
    try: 
     res = check_output(['python', 'helloworld.py']) 
     text1.insert(END, res.decode()) 
    except CalledProcessError: 
     print("File not found") 

Button(root, text="Hello", command=command).pack() 

root.mainloop() 
+0

ありがとうございました。しかし、今私は全出力をすぐに得ています。私が望むのは、GUIがターミナルに印刷するのと同じように、出力を繰り返し印刷する必要があるということです。 –

+0

私はあなたが繰り返し何を意味するのか分かりません。単に「こんにちは世界」を印刷するよりも複雑なことを考えると思います。あなたは私に例を教えていただけますか? –

+0

私はそれを介してFlashairから画像を取得するflash.pyのpythonファイルを持っています。ループを反復したい場合 –

関連する問題