想写一个文本与Tkinter输入框



只是试图让用户写下他们的名字和电子邮件地址,然后将其写入文本文件。没有弹出错误消息,只是没有写入文件。此外,消息框不会出现(+名称+ 'n' +完整的电子邮件+ 'n'),它只是出现了消息。欢呼声

import tkinter as tk #shortening tkinter
import tkinter.messagebox as box
import csv
from tkinter import *

def store_customers():
aname = name.get()
aemail = email.get()
aemailaddress = emailaddress.get()

fullemail = aemail + aemailaddress
print(fullemail)

if (name == "" or email == ""):
print('Error')
messagebox.showerror('error',"their was some issur with your information")
email.set('')
name.set('')

else:
result = messagebox.askquestion('question', 'Your about to enter yor information n' + aname + 'n' + fullemail + 'n' ) 

if (result == 'yes'):
print('here')
with open ('customersdata.txt', 'a') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([aname, fullemail])
csvfile.close()

else:
name.set('')
email.set('')


name = StringVar()
email = StringVar()
emailaddress = StringVar()

name = tk.Entry(frame4, text="",  bg = '#F0EAD6', font=('Arial',24)  )
name.place(x= 400, y = 600)
email = tk.Entry(frame4, text="",  bg = '#F0EAD6', font=('Arial',24) )
email.place(x= 400, y = 660)
list1 = ['@yahoo.com','@bing.com','@jpc.vic.edu.au', '@gmail.com', '@hotmail.com' ]
emailaddres= OptionMenu(frame4,emailaddress,*list1)
emailaddres.config(height = 2 )
emailaddress.set('@***.***.edu.au')
emailaddres.place(x= 685, y= 660)
storebtn = tk.Button(frame4, text = 'complete', bg= '#F0EAD6', font=('Arial',24), command = store_customers)
storebtn.place (x = 430, y= 700)

tk.Label(frame4, text= "Your name", bg= '#F0EAD6', font=('Arial',24)).place(x = 260, y = 600)
tk.Label(frame4, text= "email address", bg= '#F0EAD6', font=('Arial',24)).place(x = 240, y = 660)
frame4.mainloop()

使用Tkinter编写代码。

import csv
import tkinter
import tkinter.messagebox

def save():
nome_to_save = namevalue.get()
email_to_save = emailvalue.get()
if len(nome_to_save) == 0 or len(email_to_save) == 0:
tkinter.messagebox.showinfo('Error', 'You need to enter name and e-mail!')
else:
with open('customersdata.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([nome_to_save, email_to_save])
csvfile.close()
tkinter.messagebox.showinfo('Success', f'Information of {nome_to_save} saved!')

window = tkinter.Tk()
window.title("Write CSV")
namevalue = tkinter.StringVar()
emailvalue = tkinter.StringVar()
tkinter.Label(window, text = "Name").grid(row = 0)
name = tkinter.Entry(window, textvariable=namevalue).grid(row = 0, column = 1)
tkinter.Label(window, text = "E-mail").grid(row = 1)
email = tkinter.Entry(window, textvariable=emailvalue).grid(row = 1, column = 1)
tkinter.Button(window, text="Save", command = save).grid(row = 2)
tkinter.mainloop()

您可能会收到一些错误,对吗?

已导入tkinter.messageboxbox。因此用box.showerrorbox.askquestion代替messagebox.showerrormessagebox.askquestion

PS:我是新来的,所以我不能评论。对不起!

这段代码可以帮助你。

import csv
def store_customers(name, email):
if name == "" or email == "":
print('Error, i need name and email')
else:
with open('customersdata.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([name, email])
csvfile.close()
#store_customers('', '')
store_customers('Diego', 'diego@false.io')
store_customers('Ana Maria', 'ana.maria@myemail.io')
store_customers('Julia Neau', 'jneau@cool.io')

相关内容

  • 没有找到相关文章

最新更新