python gui to input data to a onedrive online excel sheet - python

so is it possible to make a python gui that can input data into a one drive shared excel spreadsheet ? and if so please can someone help me as this is my code so far
from numbers import Number
import tkinter
from tkinter import ttk
def enter_data():
Cardnumber = Card_number_entry.get()
Year = Year_entry.get()
Name = Name_entry.get()
Set = Set_entry.get()
Refractor = Refractor_entry.get()
Cert = Cert_entry.get()
Grade = Grade_entry.get()
print("Card Number:", Cardnumber)
print("Year:", Year)
print("Name:", Name)
print("Set:", Set)
print("Refractor:", Refractor)
print("Cert:", Cert)
print("Grade:", Grade)
window = tkinter.Tk()
window.title("MGC POPULATION REPORT")
frame = tkinter.Frame(window)
frame.pack()
card_info_frame =tkinter.LabelFrame(frame, text="Card Infomation")
card_info_frame.grid(row=0, column=0, padx=300, pady=35)
Card_number_label = tkinter.Label(card_info_frame, text="Card Number")
Card_number_label.grid(row=0, column=0, padx=10, pady=1)
Year_label = tkinter.Label(card_info_frame, text="Year")
Year_label.grid(row=2, column=0, padx=10, pady=1)
Name_label = tkinter.Label(card_info_frame, text="Name")
Name_label.grid(row=4, column=0, padx=10, pady=1)
Set_label = tkinter.Label(card_info_frame, text="Set")
Set_label.grid(row=6, column=0, padx=10, pady=1)
Refractor_label = tkinter.Label(card_info_frame, text="Refractor")
Refractor_label.grid(row=8, column=0, padx=10, pady=1)
Cert_label = tkinter.Label(card_info_frame, text="Certification (#)")
Cert_label.grid(row=10, column=0, padx=10, pady=1)
Grade_label = tkinter.Label(card_info_frame, text="Grade")
Grade_label.grid(row=12, column=0, padx=10, pady=1)
Card_number_entry = tkinter.Entry(card_info_frame)
Year_entry = tkinter.Entry(card_info_frame)
Name_entry = tkinter.Entry(card_info_frame)
Set_entry = tkinter.Entry(card_info_frame)
Refractor_entry = tkinter.Entry(card_info_frame)
Cert_entry = tkinter.Entry(card_info_frame)
Grade_entry = tkinter.Entry(card_info_frame)
Card_number_entry.grid(row=1, column=0)
Year_entry.grid(row=3, column=0)
Name_entry.grid(row=5, column=0)
Set_entry.grid(row=7, column=0)
Refractor_entry.grid(row=9, column=0)
Cert_entry.grid(row=11, column=0)
Grade_entry.grid(row=13, column=0)
button = tkinter.Button(frame, text="Enter Data", command= enter_data)
button.grid(row=15, column=0, sticky="news", padx=300, pady=35)
window.mainloop()
i dont even know where to start lol, so is it possible to make a python gui that can input data into a one drive shared excel spreadsheet ? and if so please can someone help me as this is my code so far

Related

Tidy layout in python tkinter

I'm making an application that allows the user to enter in information for sales at a party hire store and I've come across an issue.
Edit: I've tried using sticky and many other placement tools but nothing has seemed to work so far.
When I append more than one set of data to my dictionary and then press the print results button, all of the entered data is really close to each other.
Here is the code:
# Party Gear Hire Shop
from tkinter import *
# Makes graphics better, if causing issues feel free to remove (up to dashed line) #
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(True)
# ---------------------------------------------------------------------------------#
# Data
# Table where dataNeeded is kept
dataNeeded = {}
type(dataNeeded)
dataNeeded["Position"] = []
dataNeeded["fName"] = []
dataNeeded["lName"] = []
dataNeeded["item"] = []
dataNeeded["amount"] = []
trackerNum = 0
# Commands
def doAppend():
global trackerNum
global dataNeeded
trackerNum += 1
dataNeeded["Position"] += [trackerNum]
dataNeeded["fName"] += [nameFirstEntry.get()]
dataNeeded["lName"] += [nameLastEntry.get()]
dataNeeded["item"] += [itemHiredEntry.get()]
dataNeeded["amount"] += [amountHiredEntry.get()]
def doPrintResults():
resultsWindow = Toplevel(root)
resultsWindow.title("Receipts")
resultsFrame = LabelFrame(resultsWindow, text="Current Receipts", padx=10, pady=10)
resultsFrame.grid(row=0, column=0, columnspan=3, rowspan=5, padx=10, pady=10)
resultsHeader = Label(resultsFrame, text="Receipt No:", padx=2.5, pady=2.5)
resultsHeader.grid(row=0, column=0, padx=2.5, pady=2.5)
resultsHeader1 = Label(resultsFrame, text="First Name:", padx=2.5, pady=2.5)
resultsHeader1.grid(row=1, column=0, padx=2.5, pady=2.5)
resultsHeader1_5 = Label(resultsFrame, text="Last Name:", padx=2.5, pady=2.5)
resultsHeader1_5.grid(row=2, column=0, padx=2.5, pady=2.5)
resultsHeader2 = Label(resultsFrame, text="Item:", padx=2.5, pady=2.5)
resultsHeader2.grid(row=3, column=0, padx=2.5, pady=2.5)
resultsHeader3 = Label(resultsFrame, text="Quantity:", padx=2.5, pady=2.5)
resultsHeader3.grid(row=4, column=0, padx=2.5, pady=2.5)
rowPlacement = -1
for x in dataNeeded:
rowPlacement += 1
nameLabel = Label(resultsFrame, text=dataNeeded[x], padx=2.5, pady=2.5)
nameLabel.grid(column=trackerNum+1, row=rowPlacement, padx=2.5, pady=2.5)
root = Tk()
root.title("Party Hire Store") # Titles window
# Entry Area -----------------------------------------------------------------------------------------------------------
entryAreaFrame = LabelFrame(root, text="Add a transaction", padx=10, pady=10)
entryAreaFrame.grid(row=0, column=0, columnspan=3, rowspan=5, padx=10, pady=10)
nameFirstLabel = Label(entryAreaFrame, text="First Name:")
nameFirstLabel.grid(row=0, column=0, padx=1, pady=1)
nameFirstEntry = Entry(entryAreaFrame)
nameFirstEntry.grid(row=0, column=1, columnspan=2, rowspan=1, padx=1, pady=1)
nameLastLabel = Label(entryAreaFrame, text="Last Name:")
nameLastLabel.grid(row=1, column=0, padx=1, pady=1)
nameLastEntry = Entry(entryAreaFrame)
nameLastEntry.grid(row=1, column=1, columnspan=2, rowspan=1, padx=1, pady=1)
itemHiredLabel = Label(entryAreaFrame, text="Item Hired:")
itemHiredLabel.grid(row=2, column=0, padx=1, pady=1)
itemHiredEntry = Entry(entryAreaFrame)
itemHiredEntry.grid(row=2, column=1, columnspan=2, rowspan=1, padx=1, pady=1)
amountHiredLabel = Label(entryAreaFrame, text="Quantity:")
amountHiredLabel.grid(row=3, column=0, padx=1, pady=1)
amountHiredEntry = Entry(entryAreaFrame)
amountHiredEntry.grid(row=3, column=1, columnspan=2, rowspan=1, padx=1, pady=1)
doAppendButton = Button(entryAreaFrame, text="Append Details", padx=2.5, pady=2.5, command=doAppend)
doAppendButton.grid(row=4, column=0, padx=5, pady=5)
doPrintButton = Button(entryAreaFrame, text="Print Receipt", padx=2.5, pady=2.5, command=doPrintResults)
doPrintButton.grid(row=4, column=1, padx=5, pady=5, columnspan=2)
root.mainloop()
Now, what I'm asking for is how to make it so that for any piece of data that is entered, When I print the results, it prints neatly on a new window, which can be closed separately.
However, any other suggestions or improvements would be greatly appreciated as I look to further my skills in python, and in programming as a whole so any help is useful.
Thanks!

How to solve the "bad window path name ".!labelframe.!canvas.!frame" error in tkinter python?

This is my code:
mycanvas = Canvas(self.search_result_frame)
mycanvas.pack(side=LEFT)
yscrollbar = ttk.Scrollbar(self.search_result_frame, orient="vertical", command=mycanvas.yview)
yscrollbar.pack(side=RIGHT, fill=Y)
mycanvas.configure(yscrollcommand=yscrollbar.set)
mycanvas.bind('<Configure>', lambda e: mycanvas.configure(scrollregion = mycanvas.bbox('all')))
self.sample_frame = Frame(mycanvas)
mycanvas.create_window((0,0), window=self.sample_frame, anchor=E)
for widget in self.search_result_frame.winfo_children():
widget.destroy()
if len(matching_bills) > 0:
for bill in matching_bills:
with open(f'{self.bill_folder}//{bill}//data//bill_details.json', 'r') as bill_json_file:
bill_details = json.loads(bill_json_file.read())
customer_name = bill_details["customer_details"][0]
payment_method = bill_details["payment_method"]
date_of_issue = bill_details["date_of_issue"]
date_of_issue = datetime.strptime(date_of_issue, "%d/%m/%Y")
date_of_issue = date_of_issue.strftime("%d %b %Y")
# # -------------------- Search Result Frame Contents
result_frame = Frame(self.sample_frame, bg=self.bg3, bd=5, relief=GROOVE)
result_frame.pack(fill=BOTH, pady=2)
result_billno_lbl = Label(result_frame, text=bill, bg=self.bg1, fg="#FFF", font=self.search_results_font1, padx=22, pady=3)
result_billno_lbl.grid(row=0, column=0, padx=50, pady=8, sticky=W)
billed_to_lbl = Label(result_frame, text=f"Billed To - {customer_name}", bg=self.bg1, fg="#FFF", font=self.search_results_font2, bd=2, relief=RAISED, padx=12, pady=3)
billed_to_lbl.grid(row=0, column=1, padx=80, sticky=W)
billed_type_lbl = Label(result_frame, text=f"Bill Type - {payment_method}", bg=self.bg1, fg="#FFF", font=self.search_results_font2, bd=2, relief=RAISED, padx=12, pady=3)
billed_type_lbl.grid(row=0, column=2, sticky=W)
issued_on_lbl = Label(result_frame, text=f"Issued On - {date_of_issue}", bg=self.bg1, fg="#FFF",
font=self.search_results_font2, bd=2, relief=RAISED, padx=12, pady=3)
issued_on_lbl.grid(row=0, column=3, padx=80, sticky=W)
view_btn = Button(result_frame, text="View", font="Comicsan 14", bd=2, relief=GROOVE, bg="#000", fg="#FFF", padx=1, command=lambda bill=bill: self.view_bill(bill))
view_btn.grid(row=0, column=4, padx=3, columnspan=2, sticky=W)
elif len(matching_bills) == 0:
for widgets in self.search_result_frame.winfo_children():
widgets.destroy()
no_result_lbl = Label(self.search_result_frame, text=f"No search result found for {bill_cat}", font=self.search_results_font1, bg=self.bg3, fg="#FFF")
no_result_lbl.pack(fill=X)
When I run it, it shows me the bad window path name ".!labelframe.!canvas.!frame error and when I try to do the same thing without object-oriented in tkinter then it works well !

Where can I edit the Copyright of my Python Bundle created with py2app?

I use py2app for the creation of stand alone apps from my python scripts. I have Tkinter used in the script called "MyApp" but I wonder where I can edit the copyright of my app? When I go to "About MyApp" from menu bar which was created by py2app, I see the Tkinter copyright.
So, what do I need to do to get my own name for my own app in py2app here?
Wishes,
Mike
from tkinter import *
from tkinter import messagebox
root = Tk()
root.wm_title("Calculate something")
root.config(background="#FFFFFF")
leftFrame = Frame(root, width=200, height=400)
leftFrame.grid(row=0, column=0, padx=10, pady=3)
leftLabel1 = Label(leftFrame, text="This app helps to calculate")
leftLabel1.grid(row=0, column=0, padx=10, pady=3)
rightFrame = Frame(root, width=200, height=400)
rightFrame.grid(row=0, column=1, padx=10, pady=3)
rightLabel1 = Label(rightFrame, text="1st number:")
rightLabel1.grid(row=0, column=1, padx=10, pady=3)
rightLabel2 = Label(rightFrame, text="2nd number")
rightLabel2.grid(row=1, column=1, padx=10, pady=3)
rightLabel3 = Label(rightFrame, text="you get")
rightLabel3.grid(row=3, column=1, padx=10, pady=25)
num1_var = DoubleVar()
num2_var = DoubleVar()
# entrys
num1_ent = Entry(rightFrame, width=30, textvariable=num1_var)
num1_ent.grid(row=0, column=2, padx=10, pady=3)
num2_ent = Entry(rightFrame, width=30, textvariable=num2_var)
num2_ent.grid(row=1, column=2, padx=10, pady=3)
def calc():
num1 = num1_var.get()
num2 = num2_var.get()
if num1 == num2:
messagebox.showerror(titel = "Error", message = "Different numbers are needed", icon = "error")
elif num1 < num2:
rightLabel4 = Label(rightFrame, width=30, text=((num2 - num1)-1))
rightLabel4.grid(row=3, column=2, padx=10, pady=25)
elif num1 > num2:
rightLabel4 = Label(rightFrame, width=30, text=((num2 - num1)-5))
rightLabel4.grid(row=3, column=2, padx=10, pady=25)
buttonFrame = Frame(rightFrame)
buttonFrame.grid(row=3, column=1, padx=10, pady=3)
B1 = Button(rightFrame, text="Do it", bg="#FF0000", width=15, command=calc)
B1.grid(row=2, column=2, padx=50, pady=10)
root.mainloop()

how to display a statement in text widget of tkinter after getting input thru get statement

from tkinter import *
def tell_story(Male):
story= "My name is" + "Male"
Frame.story_txt.delete(0.0, END)
Frame.story_txt.insert(0.0, story)
root = Tk()
root.title("game")
root.geometry("800x400")
Application = Frame(root)
Application.grid()
Lbl1 = Label(Application, text="story").grid(row=0, column=0, sticky=W)
Lbl2 = Label(Application, text="Male name").grid(row=1, column=0, sticky=W)
Male = Entry(Application).grid(row=1, column=1, sticky=W)
Button(Application, text="Click for story", command=tell_story).grid(row=11, column=0, sticky=W)
Frame.story_txt = Text(Application, width=75, height=10, wrap=WORD)
Frame.story_txt.grid(row=12, column=0, columnspan=4)
root.mainloop()
I'm unable to print story variable in text widget

Using StringVar data as a list

I've been following this website for a while. It is really helpful. So, thanks for all the useful tips.
I've been searching for this answer for the past two weeks without any success, so now I'm compelled to ask out of total frustration with my current obstacle.
How do you use StringVar as a list?
Basically, I was coding the following program for my wife. The code is pretty messy as it is my first program I've tried to code.
The idea was that I would take 3 variables; password, secret number and website and then do some work on them using if statements and lists to create a unique password.
First problem was that I couldn't restrict the character length of the entry widgets but in the end i solved that but i still want to limit the characters that can be input.
for instance in the website entry box, i want to allow only letters.
If I could convert StringVar to a list, I could do some work with messy if statements to allow only certain characters in each index but everythime i try it says that stringvars cant be used as a list.
I need them as a list to limit the type of characters that can be entered and also so that i can do work on the variables to get the final output.
CODE:
from Tkinter import *
import Tkinter
from PIL import Image, ImageTk
import os
Title= ("Helvetica", 20, "bold", "underline")
Heading= ("Courier", 20, "bold")
Body= ("Courier", 15)
Body1= ("Courier", 15, "bold")
notice= ("Courier", 10)
Body2= ("Courier", 15, "bold", "underline")
root = Tk()
root.title("Koala Series: Encrypter")
root.configure(background="#ffefd5")
root.geometry('{}x{}'.format(510, 600))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1) # not needed, this is the default behavior
root.rowconfigure(1, weight=1)
root.rowconfigure(2, weight=1)
website = StringVar()
Titlemsg = Label(root, text="Koala Encrypter", font=Title, bg="#00FA9A", relief=RAISED,)
Titlemsg.grid( row=7, pady=25, sticky=N+S+E+W)
img1 = ImageTk.PhotoImage(Image.open("koala.jpeg"))
startpic = Label(root, image = img1, relief=RIDGE )
startpic.grid( row=10,pady=25, )
Head = Label(root,anchor=CENTER,text="Welcome to the Koala Encrypter \n\n A Koala series tool that allows you\n\n to Encrypt your passwords \n\n Click \'Start Encrypting\' to continue \n\n", font=Body, bg="#ffefd5", relief=RAISED,)
Head.grid( row=14, pady=25, columnspan=2, sticky=N+S+E+W)
web = Label(root, text="WEBSITE: ", font=Body, bg="#ffefd5", justify=CENTER,)
#website.set("Enter your website here")
entry = Entry(root,textvariable=website , justify=CENTER,relief=SUNKEN,cursor="pencil", takefocus=True )
Notice1 = Label( text="Please only insert the first 5 letters of the website!!",font=notice, bg="#ffefd5", fg="#0000ff",)
passw = Label(root, text="PASSWORD: ", font=Body, bg="#ffefd5", justify=CENTER)
passwordvar= StringVar()
entry1 = Entry(root, textvariable= passwordvar, justify=CENTER,relief=SUNKEN, cursor="pencil", takefocus=True )
Notice= Label(root, text="Your password must only be 5 characters long!!", font=notice, bg="#ffefd5", fg="#0000ff", justify=CENTER)
def callback(event):
if "<0>":
top = Toplevel(bg="#ffefd5")
top.title("Koala Encrypter")
popuptitle = Label(top, text="Your secret number must be between 1 and 9:", font=Body1, fg="red", bg="#ffefd5")
popuptitle.grid(row = 2,column=0, padx=5, pady = 50,sticky=N+S+E+W)
secret = Label(root, text="SECRET NUMBER: ", font=Body, bg="#ffefd5" , justify=CENTER,)
numbervar = StringVar()
entry2 = Entry(root, textvariable=numbervar , justify=CENTER,relief=SUNKEN,cursor="pencil", takefocus=True)
entry2.bind("<0>", callback)
Notice2 = Label(root, text="your secret number must be between 1 and 9!!!", font=notice, bg="#ffefd5", fg="#0000ff", justify=CENTER)
img = ImageTk.PhotoImage(Image.open("Koalalogo.jpg"))
panel = Label(root, image = img, relief=SUNKEN)
correct= Label(root, text="Check the below details \n\n Click \'yes\' if they are correct \n\n Click \'No\' to go back \n\n", font=Body1, bg="#ffefd5")
yourwebsite = Label(root, text="The Website is :", font=Body, bg="#ffefd5")#
website1 = Label(root, font=Body2, bg="#ffefd5",fg= "#00009C", textvariable = website)#
yourpassword = Label(root, text="Your Password is:", font=Body, bg="#ffefd5")
yournumber1= Label(root, font=Body2, bg="#ffefd5",textvariable = numbervar , fg= "#00009C", )
yourpassword1 = Label(root, font=Body2, bg="#ffefd5",textvariable = passwordvar , fg= "#00009C", )
yournumber= Label(root, text="Your Secret Number is:", font=Body, bg="#ffefd5")
def restart():
Titlemsg.grid_forget()
correct.grid_forget()
yourwebsite.grid_forget()
website1.grid_forget()
yourpassword.grid_forget()
yourpassword1.grid_forget()
yournumber.grid_forget()
yournumber1.grid_forget()
panel.grid_forget()
toolbar.grid_forget()
yes.grid_forget()
no.grid_forget()
entry.delete(0,END)
entry1.delete(0,END)
entry2.delete(0,END)
Titlemsg.grid( row=7, pady=25, sticky=N+S+E+W)
startpic.grid( row=10,pady=25, )
Head.grid( row=14, pady=25, columnspan=2, sticky=N+S+E+W)
toolbar.grid( row=21, )
end.grid(column =3, row=1, sticky=N+S+E+W)
begin.grid(column =2, row=1, sticky=N+S+E+W)
def start():
#entry.destroy()
#entry1.destroy()
#entry2.destroy()
toolbar.grid_forget()
Titlemsg.grid_forget()
begin.grid_forget()
Head.grid_forget()
startpic.grid_forget()
web.grid(row=3, column=0, sticky= W+E)
entry.grid( row=3, column=1, padx=50)
passw.grid(row=10, column=0)
Notice1.grid(row=4, sticky=N+S+E+W, columnspan=2)
entry1.grid(row=10, column=1)
Notice.grid(row=11,column=0, columnspan=2,)
secret.grid(row=13, column=0)
entry2.grid( row=13, column=1)
Notice2.grid( row=14,column=0, columnspan=2,)
panel.grid(row=20,columnspan=2, pady=70)
confirm.grid(column =1, row=1)
reset.grid(column =2, row=1)
end.grid(column =3, row=1)
toolbar.grid(row=21, column=0, columnspan=2)
Titlemsg.grid(row=0, column=0, columnspan=2, sticky=E+W)
def Reset():
entry.delete(0,END)
entry1.delete(0,END)
entry2.delete(0,END)
def clear_text():
#entry.destroy()
#entry1.destroy()
#entry2.destroy()
panel.grid_forget()
entry.grid_forget()
entry1.grid_forget()
entry2.grid_forget()
web.grid_forget()
Notice.grid_forget()
passw.grid_forget()
secret.grid_forget()
Notice1.grid_forget()
Notice2.grid_forget()
confirm.grid_forget()
reset.grid_forget()
toolbar.grid_forget()
Titlemsg.grid_forget()
Titlemsg.grid(row=0, column=0, columnspan=2, sticky=E+W)
correct.grid(row=1, column=0, columnspan=2, sticky=E+W)
yourwebsite.grid(row=2,column=0,sticky=E+W, pady=5)
website1.grid(row=2, column=1, padx=65,sticky=E+W, pady=5)
yourpassword.grid(row=4, column=0,sticky=E+W, pady=5)
yourpassword1.grid(row=4, column=1, padx=65,sticky=E+W, pady=5)
yournumber.grid(row=6, column=0,sticky=E+W, pady=5)
yournumber1.grid(row=6, column=1, padx=65,sticky=E+W, pady=5)
panel.grid(row=8, column=0, columnspan=2, pady=50)
toolbar.grid(row=10, column=0, columnspan=2)
yes.grid(column =1, row=1)
no.grid(column =2, row=1)
def popup():
top = Toplevel(bg="#ffefd5")
top.title("Koala Encrypter")
popuptitle = Label(top, text="Your password is:", font=Body1, fg="red", bg="#ffefd5")
popuptitle.grid(row = 2,column=0, padx=5, pady = 50,sticky=N+S+E+W)
pwd= Label(top, font=Body2, text="password", bg="#ffefd5", fg= "#00009C", ) #textvariable = newpassword ,
pwd.grid(row= 2, column=1,sticky=E+W,padx=15)
button = Button(top, text="OK", command=top.destroy, relief=RAISED )
button.grid(column =0,columnspan=2, row=4, sticky=N+S+E+W)
def helpmsg():
top = Toplevel(bg="#ffefd5")
top.title("Koala Encrypter")
popuptitle = Label(top, text="Koala series 1.0 - Koala Encrypter", font=Title, bg="#00FA9A", relief=RAISED,)
popuptitle.grid(row = 2,column=0, padx=5, pady = 50,sticky=N+S+E+W)
pwd= Label(top, font=Body, text="Free software to help you keep your acounts safe", bg="#ffefd5")
pwd.grid(row= 1,sticky=E+W,)
Titlems = Label(top, text="Koala Encrypter", font=Title, bg="#00FA9A", relief=RAISED,)
Titlems.grid( row=0, pady=25, sticky=N+S+E+W)
button = Button(top, text="OK", command=top.destroy, relief=RAISED )
button.grid(column =0,columnspan=2, row=4, sticky=N+S+E+W)
max_len = 5
def on_write(*args):
s = website.get()
if len(s) > max_len:
website.set(s[:max_len])
website.trace_variable("w", on_write)
max_len1 = 5
def on_write(*args):
s = passwordvar.get()
if len(s) > max_len1:
passwordvar.set(s[:max_len1])
passwordvar.trace_variable("w", on_write)
max_len2 = 1
def on_write(*args):
s = numbervar.get()
if len(s) > max_len2:
numbervar.set(s[:max_len2])
numbervar.trace_variable("w", on_write)
toolbar = Frame(root)
reset = Button(toolbar, text="Reset", width=6, command=Reset, cursor="cross", relief=RAISED, takefocus=True )
end = Button(toolbar, text="Quit" ,command=root.destroy, relief=RAISED, cursor="X_cursor", takefocus=True)
end.grid(column =3, row=1, sticky=N+S+E+W)
begin = Button(toolbar, text="Start Encrypting", command=start, relief=RAISED, cursor="star",takefocus=True )
begin.grid(column =2, row=1, sticky=N+S+E+W)
confirm = Button(toolbar, text="Next", command =clear_text, cursor="star", relief=RAISED,takefocus=True )
yes = Button(toolbar, text="Yes", command =popup, cursor="star", relief=RAISED,takefocus=True )
no = Button(toolbar, text="No", command =restart, cursor="pirate", relief=RAISED, takefocus=True )
toolbar.grid( row=21, )
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Restart", command=restart)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.destroy)
helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu, )
helpmenu.add_command(label="About...", command=helpmsg)
app = root
root.mainloop()
# add functionality, fix validation
To restrict the type/number of characters that can be typed into an entry widget, you can use the entry validatecommand. Here is an example to allow only letters and another one to restrict the entry to 4 digits:
from tkinter import Tk, Entry, Label
def only_letters(action, char):
if action == "1":
# a character is inserted (deletion is 0) allow the insertion
# only if the inserted character char is a letter
return char.isalpha()
else:
# allow deletion
return True
def only_numbers_max_4(action, new_text):
if action == "1":
return new_text.isdigit() and len(new_text) <= 4
else:
return True
root = Tk()
# register validate commands
validate_letter = root.register(only_letters)
validate_nb = root.register(only_numbers_max_4)
Label(root, text="Only letters: ").grid(row=0, column=0)
e1 = Entry(root, validate="key", validatecommand=(validate_letter, '%d', '%S'))
# %d is the action code and %S the inserted/deleted character
e1.grid(row=0, column=1)
Label(root, text="Only numbers, at most 4: ").grid(row=1, column=0)
e2 = Entry(root, validate="key", validatecommand=(validate_nb, '%d', '%P'))
# %P is the new content of the entry after the modification
e2.grid(row=1, column=1)
root.mainloop()
For more details on entry validation, see http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/entry-validation.html

Categories

Resources