【发布时间】:2021-05-05 14:39:32
【问题描述】:
import tkinter as tk
import datetime
import Inputs
import os
today = datetime.datetime.now()
today = today.strftime("%Y%m%d%H%M%S")
root = tk.Tk()
root.geometry("500x600")
root.title("General Examination of the Patient")
bp = tk.StringVar()
def submited():
new_file = f"{Inputs.name.get()}_trial"
path = Inputs.path
fullpath = os.path.join(path, new_file)
with open(fullpath, "w") as gpe:
gpe.write(bp.get() + "\n")
root.quit()
print(bp.get())
ga_label = tk.Label(root, text="GA")
bp_label = tk.Label(root, text="Blood Pressure")
ga_label.grid(columnspan=3, row=0)
bp_label.grid(column=0, row=1)
bp_entry = tk.Entry(root, textvariable=bp)
bp_entry.grid(column=1, row=1)
submit_btn = tk.Button(root, text="Submit", command=submited)
submit_btn.grid(columnspan=2, row=6)
root.mainloop()
三个多小时以来,我一直试图找出我的错误。我是新手,请耐心等待。
到目前为止,我的文件正在创建中,但该文件不包含我尝试写入其中的任何数据。即使在控制台输出中,我的 bp.get() 也没有返回任何值。
就在几天前,我使用相同格式的代码让另一个类似的应用程序运行,但这不起作用。请帮忙!
这里要求的是 Inputs.py 文件代码:
import datetime
import tkinter as tk
import os
import datetime
today = datetime.datetime.now()
today = today.strftime("%Y%m%d%H%M%S")
# today = str(today)
basicdetails = tk.Tk()
# Basic patient details
name = tk.StringVar(basicdetails, value="Name")
age = tk.StringVar()
gender = tk.StringVar()
height = tk.StringVar()
weight = tk.StringVar()
diet = tk.StringVar()
ethnicity = tk.StringVar()
occupation = tk.StringVar()
residence = tk.StringVar()
contact = tk.StringVar()
path = ""
def submit():
global path
new_file = f"{name.get()}_basicdetails_{today}"
path = f"E:/PythonProjects/ambiproj/Data/{name.get()}_{today}"
try:
os.mkdir(path)
except FileExistsError:
pass
fullpath = os.path.join(path, new_file)
with open(fullpath, "w") as s:
s.write(name.get() + "\n")
s.write(age.get() + "\n")
s.write(gender.get() + "\n")
s.write(height.get() + "\n")
s.write(weight.get() + "\n")
s.write(occupation.get() + "\n")
s.write(ethnicity.get() + "\n")
s.write(residence.get() + "\n")
s.write(contact.get() + "\n")
basicdetails.quit()
# Page 1: Basic Details
# name = tk.StringVar(root, value="Name")
# age = tk.StringVar()
# gender = tk.StringVar()
# height = tk.StringVar()
# weight = tk.StringVar()
# diet = tk.StringVar()
# ethnicity = tk.StringVar()
# occupation = tk.StringVar()
# residence = tk.StringVar()
# contact = tk.StringVar()
# Page 2: Additional details (to be entered later)
namebox = tk.Entry(basicdetails, textvariable=name).grid(row=0, column=1)
agebox= tk.Entry(basicdetails, textvariable=age).grid(row=1, column=1)
genderbox = tk.Entry(basicdetails, textvariable=gender).grid(row=2, column=1)
heighbox = tk.Entry(basicdetails, textvariable=height).grid(row=3, column=1)
weightwb = tk.Entry(basicdetails, textvariable=weight).grid(row=4, column=1)
dietbox = tk.Entry(basicdetails, textvariable=diet).grid(row=5, column=1)
ethnicitybox = tk.Entry(basicdetails, textvariable=ethnicity).grid(row=6, column=1)
occubox = tk.Entry(basicdetails, textvariable=occupation).grid(row=7, column=1)
contactbox = tk.Entry(basicdetails, textvariable=contact).grid(row=8, column=1)
submitbut = tk.Button(basicdetails, text="Submit", font="Verdana", command=submit).grid(row=9, columnspan=2)
basicdetails.mainloop()
# .............................
【问题讨论】:
-
您的代码使用
Inputs。那是什么? -
这是整个脚本吗?如果没有,您是否可以在创建
bp_entry后重新分配bp? -
它怎么不“工作”?
-
Inputs 是我创建的另一个 python 文件,它使用类似的代码来获取用户的名称和其他信息。此文件导入 Inputs.name 数据以创建文件名。 @BryanOakley
-
@Barmar,是的,这是整个代码,让我感到困惑的是,当 Inputs.py 使用类似的代码并且工作正常时,它是如何不起作用的。
标签: python tkinter tkinter-entry