从一个文件到下一个文件进行选择

时间:2015-03-03 12:16:44

标签: python file tkinter choice

我试图在程序中选择一个按钮, 使用Tkinter(Windows中的python 2.7)继续下一个程序。 我没有运气就一直在google。我有可能 缺乏正确的术语流行语。 无论如何,我希望问题很清楚。谢谢。

让我试着通过以下方式进行演示: ...所以我们有两个文件:PROGRAM_1.py和PROGRAM_2.py它们将是单独的文件。

PROGRAM_1.py

from Tkinter import  *
import os
import Tkinter as tk

start_color = ["blue", "red"]
root = Tk()
root.title("please choose a starting color")
# Set background #
root.configure(background="green")
root.geometry("600x150")
# Buttons #
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=LEFT)

start_color[0] = Button(topFrame, text="Choose Blue", fg="black",     bg="white", font='bold')
start_color[1] = Button(topFrame, text="Choose Red", fg="black", bg="white",     font='bold')

start_color[0].pack(side=LEFT)
start_color[1].pack(side=LEFT)

def blue(event):
    print "u chose the blue"
    os.system("PROGRAM_2.py")
    # then exit and startup PROGRAM_2".py
start_color[0].bind("<Button-1>", blue)

def red(event):
    print "u chose the red"
    os.system("PROGRAM_2.py")
    # then exit and startup PROGRAM_2".py
start_color[1].bind("<Button-1>", red)

root.mainloop()

PROGRAM_2.py

from Tkinter import  *
import os
import Tkinter as tk

start_color = ["blue", "red"]

root = Tk()

print "your color is"
print start_color["......."]  # <--- And then the choice from PROGRAM_1.py

root.mainloop()

2 个答案:

答案 0 :(得分:1)

将选项作为命令行参数传递给PROGRAM_2.PY。您可以使用PROGRAM_2.PY列表访问sys.argv中的命令行参数。因此,在PROGRAM2.PY导入sys中,将行start_color = ["blue", "red"]替换为:

import sys

if len(sys.argv > 1):
    start_color = sys.argv[1]
.
.
.
print "your color is", start_color

PROGRAM_1.PY

def blue(event):
    print "u chose the blue"
    os.system("python PROGRAM_2.py blue")
    # then exit and startup PROGRAM_2".py

def red(event):
    print "u chose the red"
    os.system("python PROGRAM_2.py red")
    # then exit and startup PROGRAM_2".py

值得指出的是os.system()不会退出当前流程;它将给定的命令作为新进程运行并等待它终止。

答案 1 :(得分:-1)

from Tkinter import  *
import os
import Tkinter as tk
import sys

root = Tk()


if (len(sys.argv) > 1):
    start_color = sys.argv[1]

print "your color is", start_color

root.mainloop()