2015-12-01 40 views
0

我写了一个简单的报告程序,当完成时输出到文本文件。它工作正常,所以我决定以GUI形式复制程序。然而,当它将输入输出到一个文本文件时,输入的名字就是一串数字。无论选中哪个复选框,它也只打印到主日志。有人可以突出显示此程序所需的更改,以便将输入导出到未改变的txt文件,而不是数字串?非常感谢:用gui打印到txt python 2.7

from Tkinter import * 


root = Tk() 

one = Label(root, text = "welcome to maxwell's reporter program") 

reporter_name = Entry(root) 
charge_nurse = Entry(root) 


label_1 = Label(root, text = "reporter's name") 
label_2 = Label(root, text = "nurse in charge") 
label_3 = Label(root, text = "please tick the type of concern") 
capacity = Checkbutton(root, text="capacity") 
speciality = Checkbutton(root, text="speciality") 
ward = Checkbutton(root, text="ward") 
transfer = Checkbutton(root, text="transfer") 
staffing = Checkbutton(root, text="staffing") 
equipement = Checkbutton(root, text="equipement") 
other = Checkbutton(root, text="other") 
concern_body_label = Label(root, text = "please state your concern below:") 
concern_body = Text(root, height=6, width=60) 



one.grid(columnspan=2) 
label_1.grid(row=1, sticky=E) 
label_2.grid(row=2, sticky=E) 
reporter_name.grid(row=1, column=1, sticky=W) 
charge_nurse.grid(row=2, column=1, sticky=W) 
label_3.grid(columnspan=2) 
capacity.grid(row=4, sticky=W) 
speciality.grid(row=5, sticky=W) 
ward.grid(row=6, sticky=W) 
transfer.grid(row=7, sticky=W) 
staffing.grid(row=8, sticky=W) 
equipement.grid(row=9, sticky=W) 
other.grid(row=10, sticky=W) 
concern_body_label.grid(row=11, sticky=W) 
concern_body.grid(row=12, columnspan=2) 


from datetime import datetime 
now = datetime.now() 
date = '%s/%s/%s' % (now.day, now.month, now.year) 
time = '%s:%s' % (now.hour, now.minute) 

concern_body = str(concern_body) 
reporter_name = str(reporter_name) 
charge_nurse = str(charge_nurse) 


def main_function(): 
    main = [date, time, "reported by: " + reporter_name.get("1.0",END), "nurse in charge: " + charge_nurse.get("1.0",END), "statement: " + concern_body.get("1.0",END)] 
    main = str(main) 
    if capacity == True: 
     appendFile = open("Capacity.txt","a") 
     appendFile.write("\n\n" + str(main)) 
     appendFile.close() 
    if speciality == True: 
     appendFile = open("Speciality.txt","a") 
     appendFile.write("\n\n" + str(main)) 
     appendFile.close() 
    if ward == True: 
     appendFile = open("Ward.txt","a") 
     appendFile.write("\n\n" + str(main)) 
     appendFile.close() 
    if transfer == True: 
     appendFile = open("Transfer.txt","a") 
     appendFile.write("\n\n" + str(main)) 
     appendFile.close() 
    if staffing == True: 
     appendFile = open("Staffing.txt","a") 
     appendFile.write("\n\n" + str(main)) 
     appendFile.close() 
    if equipement == True: 
     appendFile = open("Equipement.txt","a") 
     appendFile.write("\n\n" + str(main)) 
     appendFile.close() 
    if other == True: 
     appendFile = open("Other.txt","a") 
     appendFile.write("\n\n" + str(main)) 
     appendFile.close() 
    appendFile = open("Main_log.txt","a") 
    appendFile.write("\n\n" + str(main)) 
    appendFile.close() 

submit_button = Button(root, text="submit concern", command = main_function) 


submit_button.grid(row=13) 



root.mainloop() 

my code here

+0

请提供[MCVE](http://stackoverflow.com/help/mcve),其中包含输入/电流输出/期望输出的示例。 – Mel

+0

我已经拍摄了我的代码的屏幕打印。它可以通过蓝色链接获得:我的代码在这里。对不起,如果它不是传统的,这是我第一次使用这个网站 – max89

+0

请编辑您的文章,并使用代码标签包含代码,请参阅此处的说明http://meta.stackoverflow.com/questions/251361/how-do- i-format-my-code-blocks – user2682863

回答

1

问题是这整个if语句:

if capacity == True: 
    ... 
if speciality == True: 
    ... 
if ward == True: 
    ... 

capacityspeciality等都是小部件。小工具永远不会是True中的值小部件可能是True,但小部件不是True,它是一个小部件。

您需要值为每个checkbutton带有可变相关联,并使用该变量的值在你的条件语句:

capacityVar = BooleanVar() 
capacity = Checkbutton(root, text="capacity", onvalue=True, offvalue=False, variable=capacityVar) 
... 
if capacityVar.get(): 
    ... 

注:checkbutton在这种特殊情况下的实际值将是1如果检查和0如果未选中,则由于窗口小部件的实现细节。没什么可担心的。