Label Not Continually Updating Tkinter - python

I have read through several answers to similar topics but none of them are relevant to my situation, I am trying to display a list on a label and whenever the button is pressed to update the list the label should update as well but it only updates when submitbtn_name is clicked. Any help appreciated I am very new to Tkinter.
My Code:
from tkinter import *
root = Tk()
root.title("Path Creator")
root.geometry('1680x1080')
root['background'] = '#aaaaaa'
list_of_paths_list = []
path_list = []
name = Entry(root, width=400)
name.insert(0, "Name")
name.pack()
def add_name():
path_list.append(name.get())
list_label = Label(root, text="path name:" + str(path_list))
list_label.pack()
submitbtn_name = Button(root, text="submit name", command=add_name)
submitbtn_name.pack()
value1 = Entry(root, width=400)
value1.insert(0, "value1")
value1.pack()
distance1 = Entry(root, width=400)
distance1.insert(0, "distance1")
distance1.pack()
def submitbtn_value_dist():
temp_list = []
temp_list.append(value1.get())
temp_list.append(distance1.get())
path_list.append(tuple(temp_list))
list_label = Label(root, text="current path: " + path_list)
list_label.pack()
submitbtn_pathpart = Button(root, text="submit path part", )
submitbtn_pathpart.pack()
root.mainloop()

Using f-string format. Added command in submitbtn_pathpart
Snippet:
def submitbtn_value_dist():
temp_list = []
temp_list.append(value1.get())
temp_list.append(distance1.get())
path_list.append(tuple(temp_list))
list_label = Label(root, text=f"current path:{path_list}")
list_label.pack()
submitbtn_pathpart = Button(root, text="submit path part", command=submitbtn_value_dist )
submitbtn_pathpart.pack()
root.mainloop()
Output for name:
Output for value and distance:

Related

How do I save my text from the textbox on tkinter to a text file?

Another noob asking for help again. Here is my code. I am trying to collect my text from the textbox. I have searched for the solution on how to save it but it just opens the save file and doesn't save anything. What I am trying to do is to save the text into a file after I've listed data using my widgets as a save file but sadly, that's the only thing that does not work for me. Perhaps I did something wrong in trying to save it. I am trying to fix my function saving_file_txt.
class OrderWindow:
def __init__(self, master):
self.master = master
self.master.title("Order Window Page")
self.master.geometry("1500x800")
self.master.configure(background="azure")
self.frame = Frame(self.master)
self.frame.pack()
# Placeholder for the information when ordering
self.prod_no_var = StringVar() # Product No input placeholder
self.product_name_var = StringVar() # Product Name input placeholder
self.quantity_var = StringVar() # Quantity input placeholder
self.prod_cost_var = StringVar() # Product Cost input placeholder
self.subtotal_var = StringVar() # Subtotal input placeholder
######################### Ordering Frame ################################
# Frame 1 - Ordering Details
self.order_detail_frame = Frame(self.frame, bg="azure")
self.order_detail_frame.grid(row=1, column=0)
self.basket_frame = Frame(self.frame)
self.basket_frame.grid(row=1, column=1)
self.heading = Label(self.order_detail_frame, text="AAT Shopping Application",
font=("arial", 15, "bold")).grid(row=0, column=0, ipadx=25)
self.order_detail_lblFrame = LabelFrame(self.order_detail_frame, text="Order Details")
self.order_detail_lblFrame.grid(row=1, column=0, ipady=50)
self.basket_heading = Label(self.basket_frame,
text="Product No\t\tProduct Name\t\tProduct Quantity\t\tProduct Price\t\tSubtotal"
).grid(row=0, column=0, columnspan=4, sticky="ne", ipadx=10)
self.basket_textbox = Text(self.basket_frame)
self.basket_textbox.grid(row=1, column=0, columnspan=4)
###########################Labels#############################
self.order_no = Label(self.order_detail_lblFrame, text="Order No").grid(row=0, column=0)
self.prod_name_lbl = Label(self.order_detail_lblFrame, text="Product Name").grid(row=1, column=0)
self.prod_qty_lbl = Label(self.order_detail_lblFrame, text="Quantity").grid(row=2, column=0)
self.prod_cost_lbl = Label(self.order_detail_lblFrame, text="Product Cost").grid(row=3, column=0)
self.subtotal_lbl = Label(self.order_detail_lblFrame, text="Sub Total").grid(row=4, column=0)
# Entry and Option Menu for ordering
########################### Product Combo Box #############################
self.prod_name_cb = ttk.Combobox(self.order_detail_lblFrame, textvariable=self.product_name_var)
self.prod_name_cb.grid(row=1, column=1, padx=35)
self.prod_name_cb["value"] = ("", "Gundam", "Starwars", "Paw Patrol", "Peppa Pig", "Cars Disney", "Teddy Bear")
self.prod_name_cb.current(0)
########################## Entry Box #############################
self.prod_no_entry = Entry(self.order_detail_lblFrame, textvariable=self.prod_no_var)
self.prod_no_entry.grid(row=0, column=1)
self.order_qty_entry = Entry(self.order_detail_lblFrame, textvariable=self.quantity_var)
self.order_qty_entry.grid(row=2, column=1)
self.order_cost_entry = Entry(self.order_detail_lblFrame, textvariable=self.prod_cost_var, state="disabled")
self.order_cost_entry.grid(row=3, column=1)
self.order_subtotal_entry = Entry(self.order_detail_lblFrame, textvariable=self.subtotal_var,
state="disabled")
self.order_subtotal_entry.grid(row=4, column=1) # Repeated line because it returns none value which gives error
########################## Buttons #############################
self.add_button = Button(self.order_detail_lblFrame, text="Add", command=self.add_item).grid(row=6, column=0)
self.delete_button = Button(self.order_detail_lblFrame, text="Delete", command=self.delete_item).grid(row=7,
column=0)
self.reset_button = Button(self.order_detail_lblFrame, text="Reset", command=self.reset_entry).grid(row=8,
column=0)
self.category_button = Button(self.order_detail_lblFrame, text="View products",
command=self.open_catalogue).grid(row=9, column=0)
self.save_basketfile_button = Button(self.order_detail_lblFrame, text="Save Reciept",
command=self.saving_file_txt
).grid(row=6, column=1)
self.pay_button = Button(self.order_detail_lblFrame, text="Checkout",
command=self.checkout_item).grid(row=7, column=1)
self.screenshot_button = Button(self.order_detail_lblFrame, text="Screenshot Window",
command=self.screenshoot_screen).grid(row=8, column=1)
self.exit_window_button = Button(self.order_detail_lblFrame, text="Exit",
command=self.exit_window).grid(row=9, column=1)
def add_item(self):
global total
item_dict = {"": 0, "Gundam": 10, "Starwars": 20, "Paw Patrol": 30, "Peppa Pig": 15, "Cars Disney": 15,
"Teddy Bear": 10}
order_no = self.prod_no_var.get()
item = self.product_name_var.get()
price = (self.prod_cost_var.get())
qty = int(self.quantity_var.get())
for product, cost in item_dict.items():
if item == product:
price = cost
total = round(price * qty, 2)
self.prod_no_var.set(int(order_no) + 1)
self.prod_cost_var.set("£" + str(price))
self.subtotal_var.set("£" + str(total))
self.basket_textbox.insert(END, self.prod_no_var.get() + "\t\t" + self.product_name_var.get() + "\t\t\t" +
self.quantity_var.get() + "\t\t" + self.prod_cost_var.get() + "\t\t" +
self.subtotal_var.get() + "\n")
def delete_item(self):
self.basket_textbox.delete("1.0", "2.0")
def reset_entry(self):
self.prod_no_var.set("")
self.product_name_var.set("")
self.quantity_var.set("")
self.prod_cost_var.set("")
self.subtotal_var.set("")
def checkout_item(self):
self.newWindow = Toplevel(self.master)
self.app = PaymentWindow(self.newWindow)
def open_catalogue(self):
self.newWindow = Toplevel(self.master)
self.app = CatalogueWindow(self.newWindow)
def exit_window(self):
self.master.destroy()
def screenshoot_screen(self):
window_screenshot = pyautogui.screenshot()
file_path = filedialog.asksaveasfilename(defaultextension=".png")
window_screenshot.save(file_path)
def saving_file_txt(self):
filetext = self.basket_textbox.get("1.0", "end-1c")
save_text = filedialog.asksaveasfilename(
defaultextension="txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
)
filetext.save(save_text)
I too am a newb , but this seems crazy confusing to utilize a class for a new window in tkinter.
Maybe try to simplify by utilizing tkinter.Toplevel() and then sourcing your logical functions from there. eliminate the need for so many self calls and just update forms after submission and save. A simplified example below.
# import libraries (i use as tk to reduce the full import on launch)
import tkinter as tk
client_orders = [] # List of orders
def new_order_popup():
new_order_window.deiconify() # used to open the new order window (toplevel window)
class NEW_ORDER: # new order class, simplified for example)
def __init__(self, product_num, product_name, product_price):
self.product_num = product_num
self.product_name = product_name
self.product_price = product_price
def new_order(product_num, product_name, product_price): # creates each new order when
called.
new_order_window.deiconify()
input_product_num = pro_num_entry.get()
input_product_name = pro_name_entry.get()
input_product_price = float(pro_price_entry.get())
newOrder = NEW_ORDER(input_product_num, input_product_name, input_product_price)
return newOrder
def sub_(): # action when button pressed for submit/save
customer_order = new_order(product_num=str, product_name=str, product_price=float)
price_str = str(customer_order.product_price)
client_orders.append(customer_order)
order_string = (customer_order.product_num,' : ', customer_order.product_name,' :
', price_str,'\n')
print(order_string)
txt_file = open(f'text_doc.txt', 'a')
txt_file.writelines(order_string)
txt_file.close()
def sub_ex(): # same as other button but closes toplevel window.
customer_order = new_order(product_num=str, product_name=str, product_price=float)
price_str = str(customer_order.product_price)
client_orders.append(customer_order)
order_string = (customer_order.product_num, ' : ', customer_order.product_name, '
: ', price_str, '\n')
print(order_string)
txt_file = open(f'text_doc.txt', 'a')
txt_file.writelines(order_string)
txt_file.close()
new_order_window.withdraw()
#main window tkinter code. (at bottom eleviates complicated poiubters abd calls)
main = tk.Tk()
main.geometry('300x100')
main.title('Some Ordering System')
#button that does something
new_order_button = tk.Button(main)
new_order_button.configure(text='NewOrder', command = lambda: new_order_popup())
new_order_button.pack(pady=25)
#secondary window
new_order_window = tk.Toplevel(main)
new_order_window.geometry('300x200')
new_order_window.withdraw()
list_label = tk.Label(new_order_window)
list_label.configure(text='Prod. Num.\nProd. Name.\nProd. Price.')
list_label.place(anchor=tk.NW, x=15, y=15)
pro_num_entry = tk.Entry(new_order_window)
pro_num_entry.configure(width=20)
pro_num_entry.place(anchor=tk.NW, x=100, y=15)
pro_name_entry = tk.Entry(new_order_window)
pro_name_entry.configure(width=20)
pro_name_entry.place(anchor=tk.NW, x=100, y=35)
pro_price_entry = tk.Entry(new_order_window)
pro_price_entry.configure(width=20)
pro_price_entry.place(anchor=tk.NW, x=100, y=55)
submit_button = tk.Button(new_order_window)
submit_button.configure(text='Submit', command = lambda: sub_())
submit_button.place(anchor=tk.NW, x=35, y=100)
submit_exit_button = tk.Button(new_order_window)
submit_exit_button.configure(text='Submit and exit', command = lambda: sub_ex())
submit_exit_button.place(anchor=tk.NW, x=100, y=100)
main.mainloop()
launch: (mainwindow)
click:(toplevel)
output:(txt_file)
Hope this helps a bit. sometimes the answer is in the nested mess with my projects.
There is no save() function for string (filetext). You need to open the selected file (save_text) in write mode and save the text content to the file:
def saving_file_txt(self):
filetext = self.basket_textbox.get("1.0", "end-1c")
save_text = filedialog.asksaveasfilename(
defaultextension="txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
)
if save_text:
with open(save_text, "w") as f:
f.write(filetext)
There's no save() method that can be used to save data; instead, you have to "manually" save the data. You can use the following commands to save the data:
open(filename, 'w').write(data)

How to load a url which is in entry widget of tkinter which is linked with saved user input of a list in list-box?

My Problem is described in these following steps:
1. Opened Application
2. Typed name = Mike ; id = 11 ; url = www.google.com
3. Clicked on "Add"
4. Closed the application.
5. Again Run the application.
6. Shows "Mike = 11" in the list box.
7. But when I select "Mike = 11" and click on "load" , it does not take me to "www.google.com", Why?
Please give me some solutions about how can I open a URL attaching with saved list.
Please HELP me!!
from tkinter import*
import webbrowser
def add():
name = entry1.get()
id = entry2.get()
listbox.insert(END, name+ " : " +id)
def delete():
select = listbox.curselection()
index = select[0]
listbox.delete(index)
def save():
with open("file.txt","w") as f:
for i in listbox.get(0,END):
f.write(i+"\n")
#f.close()
def load():
url = entry3.get()
select=listbox.curselection()
index=select[0]
webbrowser.open(index)
read = open("file.txt","r")
data_list = read.readlines()
read.close()
data_list = [data.rstrip() for data in data_list]
win = Tk()
win.title("Class")
frame1=Frame(win)
frame2=Frame(win)
frame1.pack()
frame2.pack()
label1 = Label(frame1,text="Name : ")
label1.grid(row=0,column=0)
label2 = Label(frame1,text="Id : ")
label2.grid(row=1,column=0)
label3 = Label(frame1,text="Url : ")
label3.grid(row=2,column=0)
name = StringVar()
entry1 = Entry(frame1,textvariable=name)
entry1.grid(row=0,column=1)
id = StringVar()
entry2 = Entry(frame1,textvariable=id)
entry2.grid(row=1,column=1)
url = StringVar()
entry3 = Entry(frame1,textvariable=url)
entry3.grid(row=2,column=1)
scrollbar = Scrollbar(frame2,orient=VERTICAL)
listbox = Listbox(frame2,selectmode=EXTENDED,yscrollcommand=scrollbar.set,width=60)
listbox.pack()
scrollbar.config(command=listbox)
for item in data_list:
listbox.insert(END,item)
button1 = Button(frame2,text="Add",command=add)
button1.pack()
button2 = Button(frame2,text="Delete",command=delete)
button2.pack()
button3 = Button(frame2,text="Save to File",command=save)
button3.pack()
button4 = Button(frame2,text="Load Url",command=load)
button4.pack()
win.mainloop()
You need to use two list.one list saves data in the file,this could be seen in the Listbox.The another list is to save the url in the file.this couldn't be seen.And also you need to synchronize them.(save(),delete(),add() need to operate both Listbox widget and the list).A minimal example.:
from tkinter import*
import webbrowser
def add():
name = entry1.get()
id = entry2.get()
url = entry3.get()
url_list.append(url)
listbox.insert(END, name+ " : " +id)
def delete():
select = listbox.curselection()
index = select[0]
url_list.pop(index)
listbox.delete(index)
def save():
with open("file.txt","w") as f:
for i,j in zip(listbox.get(0,END),url_list):
f.write(f"{i} Url:{j}\n")
def load():
select=listbox.curselection()
index=select[0]
load_url = url_list[index]
webbrowser.open(load_url)
read = open("file.txt","r")
data_url_list = read.readlines()
read.close()
data_list = [data.rstrip().split("Url")[0] for data in data_url_list]
url_list = [data.rstrip().split("Url:")[1] for data in data_url_list]
win = Tk()
win.title("Class")
frame1=Frame(win)
frame2=Frame(win)
frame1.pack()
frame2.pack()
label1 = Label(frame1,text="Name : ")
label1.grid(row=0,column=0)
label2 = Label(frame1,text="Id : ")
label2.grid(row=1,column=0)
label3 = Label(frame1,text="Url : ")
label3.grid(row=2,column=0)
name = StringVar()
entry1 = Entry(frame1,textvariable=name)
entry1.grid(row=0,column=1)
id = StringVar()
entry2 = Entry(frame1,textvariable=id)
entry2.grid(row=1,column=1)
url = StringVar()
entry3 = Entry(frame1,textvariable=url)
entry3.grid(row=2,column=1)
scrollbar = Scrollbar(frame2,orient=VERTICAL)
listbox = Listbox(frame2,selectmode=EXTENDED,yscrollcommand=scrollbar.set,width=60)
listbox.pack()
scrollbar.config(command=listbox)
for item in data_list:
listbox.insert(END,item)
button1 = Button(frame2,text="Add",command=add)
button1.pack()
button2 = Button(frame2,text="Delete",command=delete)
button2.pack()
button3 = Button(frame2,text="Save to File",command=save)
button3.pack()
button4 = Button(frame2,text="Load Url",command=load)
button4.pack()
win.mainloop()
In this example,the format in the file:
name : id Url:xxxxxx
You also could use another way to save them and read them.

Get Checkbox states in array - Tkinter

I just started coding less than 3 weeks ago (took a class in Lynda) and now working on a GUI that enable user to tick/untick a lists. I manage to get it done but in inefficient workaround (someone gave me a heads up on this).
What i have done basically calling a variables for each checkbox and insert the checkbox states into it. So if I have 100 of checkboxes in the list, I will need to create 100 variables. Below is the working code that I wrote.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
var1t1 = tk.IntVar()
var2t1 = tk.IntVar()
var3t1 = tk.IntVar()
var_name = []
root.title("Testing Checkbox")
root.geometry("200x150")
def boxstates_t1():
var_name = [var1t1.get(), var2t1.get(), var3t1.get()]
print(var_name)
# -----------------Checkbox-----------------
labelName = tk.Label(root, text = "Name")
labelName.pack(anchor = tk.W)
check1_t1 = ttk.Checkbutton(root, text = "Mike", variable = var1t1)
check1_t1.pack(anchor = tk.W)
check2_t1 = ttk.Checkbutton(root, text = "Harry", variable = var2t1)
check2_t1.pack(anchor = tk.W)
check3_t1 = ttk.Checkbutton(root, text = "Siti", variable = var3t1)
check3_t1.pack(anchor = tk.W)
# -----------------Button-----------------
btn2 = ttk.Button(root, text="Show", command = boxstates_t1)
btn2.pack(side=tk.RIGHT)
root.mainloop()
Then i googled around and found few code that lead me to use for loop to print the list. I initialize the var_name = [] so that it will capture each checkbox state from the list.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
var1 = tk.IntVar()
var_name = []
root.title("Testing Checkbox")
root.geometry("200x150")
def boxstates():
var_name = var1.get()
print(var_name)
# ------------------Chekbox-------------
name1 = ["Mike", "Harry", "Siti"]
labelName = tk.Label(root, text = "Name")
labelName.pack(anchor = tk.W)
for checkboxname in name1:
check1_t1 = ttk.Checkbutton(root, text=checkboxname, variable=var1)
check1_t1.pack(anchor = tk.W)
# ------------------Button-------------
btn2 = ttk.Button(root, text="Show", command = boxstates)
btn2.pack(side=tk.RIGHT)
root.mainloop()
But I was unable to tick the checkbox independently and the print out result also comes back as if it's in single variable. Am I doing the array var_name = [] wrongly? I'm currently lost and have no clue where to head next.
Any advise is greatly appreciate.
Try this:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Testing Checkbox")
root.geometry("200x150")
def boxstates():
for var in vars:
print (var.get())
names = ["Mike", "Harry", "Siti"]
labelName = tk.Label(root, text = "Name")
labelName.pack(anchor = tk.W)
vars = []
for i, checkboxname in enumerate(names):
vars.append(tk.IntVar())
check = ttk.Checkbutton(root, text=checkboxname, variable=vars[i])
check.pack(anchor = tk.W)
btn = ttk.Button(root, text="Show", command = boxstates)
btn.pack(side=tk.RIGHT)
root.mainloop()
import tkinter as tk
from tkinter import ttk
def boxstates():
finalValue = []
for x in checkboxList:
finalValue.append(x.get())
print(finalValue)
root = tk.Tk()
root.title("Testing Checkbox")
root.geometry("200x150")
# ------------------Chekbox-------------
checkboxList = [tk.IntVar(), tk.IntVar(), tk.IntVar()] # if you want to add more you can either append to it or declare it before runtime
name1 = ["Mike", "Harry", "Siti"]
labelName = tk.Label(root, text = "Name")
labelName.pack(anchor = tk.W)
def createCheckboxes():
for x, y in zip (checkboxList, name1):
check1_t1 = ttk.Checkbutton(root, text=y, variable=x)
check1_t1.pack(anchor = tk.W)
# ------------------Button-------------
btn2 = ttk.Button(root, text="Show", command = boxstates)
btn2.pack(side=tk.RIGHT)
createCheckboxes()
root.mainloop()

How to use class and self here to get two different entries?

With my current code, it does not matter whether I click on "Input Folder" - Change or "JukeBox" change the result always gets displayed in "JukeBox" entry. This is incorrect, using class and self how can I change the code to display result from "Input Folder" - Change in "Input Folder" entry and the result from "Jukbox" - Change in "Jukebox" entry?
Also, how can I save the selected folders to a file so that it is there on app exit and re open?
My code:
import os
from tkinter import *
from tkinter import filedialog
inPut_dir = ''
jukeBox_dir = ''
def inPut():
opendir = filedialog.askdirectory(parent=root,initialdir="/",title='Input Folder')
inPut_dir = StringVar()
inPut_dir = os.path.abspath(opendir)
entry.delete(0, END)
entry.insert(0, inPut_dir)
def jukeBox():
opendir = filedialog.askdirectory(parent=root,initialdir="/",title='JukeBox')
jukeBox_dir = StringVar()
jukeBox_dir = os.path.abspath(opendir)
entry.delete(0, END)
entry.insert(0, jukeBox_dir)
root = Tk()
root.geometry("640x240")
root.title("Settings")
frametop = Frame(root)
framebottom = Frame(root)
frameright = Frame(framebottom)
text = Label(frametop, text="Input Folder").grid(row=5, column=2)
entry = Entry(frametop, width=50, textvariable=inPut_dir)
entry.grid(row=5,column=4,padx=2,pady=2,sticky='we',columnspan=20)
text = Label(frametop, text="JukeBox").grid(row=6, column=2)
entry = Entry(frametop, width=50, textvariable=jukeBox_dir)
entry.grid(row=6,column=4,padx=2,pady=2,sticky='we',columnspan=20)
ButtonA = Button(frametop, text="Change", command=inPut).grid(row=5, column=28)
ButtonB = Button(frametop, text="Change", command=jukeBox).grid(row=6, column=28)
ButtonC = Button(frameright, text="OK").grid(row=5, column=20, padx=10)
ButtonD = Button(frameright, text="Cancel").grid(row=5, column=15)
frametop.pack(side=TOP, fill=BOTH, expand=1)
framebottom.pack(side=BOTTOM, fill=BOTH, expand=1)
frameright.pack(side=RIGHT)
root.mainloop()
See attached image:enter image description here
Your code has both:
entry = Entry(frametop, width=50, textvariable=inPut_dir)
entry.grid(row=5,column=4,padx=2,pady=2,sticky='we',columnspan=20)
and
entry = Entry(frametop, width=50, textvariable=jukeBox_dir)
entry.grid(row=6,column=4,padx=2,pady=2,sticky='we',columnspan=20)
with jukeBox_dir/row 6 overriding inPut_dir/row 5
Therefore, in def input:
where you have:
entry.insert(0, inPut_dir)
You'll get the result in row 5 (jukebox_dir)

Tkinter python: issue with clearing out the widget

Someone please help me figure this out.
I have the following problem in Tkinter Python. The problem is that the text is overlaying with the previous text. and/or is copying below that.
I've tried to use label.config(root).pack() instead of Label(root, text="").pack()
This kinda solves the problem it starts to write over the previous text but there are 2 problems with this.
1: the text old text/Entrys all still there, it just overlays.
2: this only works with label.config, and I would also like this to work with buttons and Entrys(textbox)
I've also tried .pack_forget() and .destroy() but unfortunately it did nothing.
CODE
from tkinter import *
import pickle
import time
import os
def new_profile():
global Var1, Var2
var1 = StringVar()
var2 = StringVar()
Label(root, text="Create a new profile").pack()
Label(root, text="User Name").pack()
Entry(root, textvariable=var1).pack()
Label(root, text="Password").pack()
Entry(root, textvariable=var2).pack()
Button(root, text="Create", command=create_profile).pack()
Var1, Var2 = var1, var2
return
def create_profile():
text1 = Var1.get()
text2 = Var2.get()
print(text1)
dict = {}
dict['Name'] = text1
dict['Password'] = text2
pickle.dump(dict, open("test.txt", "wb"))
dict1 = pickle.load(open("test.txt", "rb"))
if dict1['Name'] == text1 and dict1['Password'] == text2:
Label(root, text="").pack()
Label(root, text="Profile creation successful", ).pack()
Label(root, text="Name:" + " " + text1).pack()
Label(root, text="Password:" + " " + text2).pack()
else:
Label(root, text="Something went wrong while creating your profile.", ).pack()
return
def load_profile():
select = "Load profile.."
label.config(text=select)
return
root = Tk()
root.geometry("500x400+300+300")
root.title("client")
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New Profile", command=new_profile)
filemenu.add_command(label="Load Profile", command=load_profile)
menubar.add_cascade(label="Profile Options", menu=filemenu)
root.config(menu=menubar)
label = Label(root)
label.pack()
root.mainloop()
create a array such as
on_screen = []
at the start then name your widgets and and add them to the array
password_label = Label(root, text="Password").pack()
password = Entry(root, textvariable=var2).pack()
on_screen.append(password)
on_screen.append(password_label)
then use a for loop to destroy all the widgets in the array
for w in on_screen:
w.destroy()

Categories

Resources