使用gui python 2.7打印到txt

时间:2015-12-01 09:06:30

标签: python-2.7 tkinter

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

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

1 个答案:

答案 0 :(得分: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():
    ...

注意:在特定情况下,检查按钮的实际值如果选中则为1,如果未选中则为0,原因是窗口小部件的实现细节。没什么值得担心的。