Generating CSV file with tkinter window - python

i'm trying to create CSV generator that will generate csv file for the list of users i will provide with to him.
Generated row should be looking like this:
add;Username#something.com;Rolename1
add;Username#something.com;Rolename2
add;Username2#something.com;Rolename1
add;Username2#something.com;Rolename2
This is if i select checkboxes for roles 1 and 2 it will add rows with both roles for the user.
So far i got to file getting generated but my main issue right now is that i don't know how to make it read users from tkinter Text and then saving that into csv. I would want it to only accept A-Z and 0-9 for usernames.
Here's my code so far:
from tkinter import *
from tkinter import ttk
import datetime
import pandas as pd
root = Tk()
root.geometry('500x500')
root.title("Csv generator")
#Creator name
creator = Entry(root, width=23)
creator.grid(column=1,row=0)
creator.insert(0, "Enter here your username")
#Functions entry
functionLabel = Label(root, text="FUNCTION", width=10).grid(column=0, row=1, padx=10)
clickFunction = StringVar()
clickFunction.set("Select your function")
Function = OptionMenu(root, clickFunction, "add", "chg", "cho", "del")
Function.grid(column=0, row=2, padx=10)
#Username list
usernameLabel = Label(root, text="USERNAME",width=10)
usernameLabel.grid(column=1, row=1, padx=10)
usernameList = Text(root, width=10, height=4)
usernameList.grid(column=1, row=1, padx=10, rowspan=4)
#Role List
rolesLabel = Label(root, text="ROLES", width=10)
rolesLabel.grid(column=2, row=1, padx=10)
rolesCheck1 = StringVar()
role1 = Checkbutton(root, text="Role01", variable=rolesCheck1, onvalue="Role01", offvalue="")
role1.grid(column=2, row=2, padx=10)
rolesCheck2 = StringVar()
role2 = Checkbutton(root, text="Role02", variable=rolesCheck2)
role2.grid(column=2, row=3, padx=10)
rolesCheck3 = StringVar()
role3 = Checkbutton(root, text="Role1", variable=rolesCheck3)
role3.grid(column=2, row=4, padx=10)
rolesCheck4 = StringVar()
role4 = Checkbutton(root, text="Role2", variable=rolesCheck4)
role4.grid(column=2, row=5, padx=10)
#Read Textbox
def usersout():
inputvalue = usernameList.get("1.0","end-1c")
print(inputvalue)
#CSV Creator part
def csvGenerate():
basename= creator.get()
suffix = datetime.datetime.now().strftime("%Y%m%d%H%M.csv")
filename = "_".join([basename, suffix]) # e.g. 'Creator_120508171442'
df = pd.read_csv('Template.csv')
df2 = df.append(usernameList, ignore_index=True)
df.to_csv(filename, index=False, header=False)
#Generate button
GENERATE = Button(root, text="GENERATE", command=csvGenerate).grid(column=0, row=6, columnspan=3)
root.mainloop()
Window
EDIT 20.10.2021:
Changed my code from csv to pandas as suggested but still no idea how to proceed.

I would suggest to create the checkbuttons using for loop and store those StringVars in a list which can be used easily latter to determine which roles are selected.
Note that csv module is used instead of pandas:
from tkinter import *
from tkinter import ttk
import datetime
import csv
root = Tk()
root.geometry('500x500')
root.title("Csv generator")
#Creator name
creator = Entry(root, width=23)
creator.grid(column=1, row=0)
creator.insert(0, "Enter here your username")
#Functions entry
Label(root, text="FUNCTION", width=10).grid(column=0, row=1, padx=10)
clickFunction = StringVar(value="Select your function")
OptionMenu(root, clickFunction, "add", "chg", "cho", "del").grid(column=0, row=2, padx=10)
#Username list
Label(root, text="USERNAME", width=10).grid(column=1, row=1, padx=10)
usernameList = Text(root, width=10, height=4)
usernameList.grid(column=1, row=1, padx=10, rowspan=4)
#Role List
Label(root, text="ROLES", width=10).grid(column=2, row=1, padx=10)
role_vars = [] # for storing those StringVars
# create the checkbuttons
for row, role in enumerate(("Role01", "Role02", "Role1", "Role2"), 2):
var1 = StringVar()
Checkbutton(root, text=role, variable=var1, onvalue=role, offvalue='').grid(row=row, column=2, padx=10, sticky='w')
role_vars.append(var1)
#CSV Creator part
def csvGenerate():
basename = creator.get()
suffix = datetime.datetime.now().strftime("%Y%m%d%H%M.csv")
filename = "_".join([basename, suffix]) # e.g. 'Creator_202110211234.csv'
with open(filename, "w", newline="") as file:
writer = csv.writer(file)
func = clickFunction.get() # use the StringVar instead
# get user list
users = usernameList.get('1.0', 'end-1c').splitlines()
# get selected roles
roles = [role.get() for role in role_vars if role.get()]
for user in users:
for role in roles:
writer.writerow([func, user, role])
#Generate button
Button(root, text="GENERATE", command=csvGenerate).grid(column=0, row=6, columnspan=3)
root.mainloop()

You can use pandas to generate csv
import pandas as pd
data = {'Product': ['Desktop Computer','Tablet','Printer','Laptop'],
'Price': [850,200,150,1300]
}
df = pd.DataFrame(data, columns= ['Product', 'Price'])
df.to_csv (r'C:\Users\Ron\Desktop\export_dataframe.csv', index = False, header=True)
print (df)
edit: you also have the csv package to create CSVs, but I prefer pandas as it is very simple to create a dict and transform it into DataFrame -> csv.

Related

tkinter print values on "enter" press

I need to print values on Screen When "Enter is pressed"
but not able to do so...
Currently its done with onClick method on button click...
How can Implement it?
I have tried
root.bind('<Return>', getvals) but didn't work i get error
return self.func(*args) TypeError: getvals() takes 0 positional
arguments but 1 was given
from tkinter import *
root = Tk()
def getvals():
print("Submitting form")
print(f"{namevalue.get(), phonevalue.get(), gendervalue.get(), emergencyvalue.get(), paymentmodevalue.get(), foodservicevalue.get()} ")
root.geometry("644x344")
#Heading
Label(root, text="Welcome to Harry Travels", font="comicsansms 13 bold", pady=15).grid(row=0, column=3)
#Text for our form
name = Label(root, text="Name")
phone = Label(root, text="Phone")
gender = Label(root, text="Gender")
emergency = Label(root, text="Emergency Contact")
paymentmode = Label(root, text="Payment Mode")
#Pack text for our form
name.grid(row=1, column=2)
phone.grid(row=2, column=2)
gender.grid(row=3, column=2)
emergency.grid(row=4, column=2)
paymentmode.grid(row=5, column=2)
# Tkinter variable for storing entries
namevalue = StringVar()
phonevalue = StringVar()
gendervalue = StringVar()
emergencyvalue = StringVar()
paymentmodevalue = StringVar()
foodservicevalue = IntVar()
#Entries for our form
nameentry = Entry(root, textvariable=namevalue)
phoneentry = Entry(root, textvariable=phonevalue)
genderentry = Entry(root, textvariable=gendervalue)
emergencyentry = Entry(root, textvariable=emergencyvalue)
paymentmodeentry = Entry(root, textvariable=paymentmodevalue)
# Packing the Entries
nameentry.grid(row=1, column=3)
phoneentry.grid(row=2, column=3)
genderentry.grid(row=3, column=3)
emergencyentry.grid(row=4, column=3)
paymentmodeentry.grid(row=5, column=3)
#Checkbox & Packing it
foodservice = Checkbutton(text="Want to prebook your meals?", variable = foodservicevalue)
foodservice.grid(row=6, column=3)
#Button & packing it and assigning it a command
Button(text="Submit to Harry Travels", command=getvals ).grid(row=7, column=3)
#enter press displays the value of the entry
# root.bind('<Return>', getvals)
root.mainloop()
Use:
def getvals(event=None):
and un-comment
root.bind('<Return>', getvals)
That's it ...
P.S. The 'trick' is to allow one parameter in getvals, but preset it with a default value in case there is no parameter passed to the function. This way can getvals be used for both button click and key-press.

Values not stored in Tkinter Variables

In my code, I have tried to get the user input through text fields, store them in variables and finally print them in a tabular form.
The problem I am facing is that none of the values I enter through the text fields get displayed; when I try printing the variables, they come up empty.
Here's part of my code:
# SPASC
from tkinter import *
import tkinter as tk
import tkinter.ttk as tktrv
root = tk.Tk()
root.title("SPASC")
root.geometry("410x400")
lb1 = Label(root, text="SPASC \n Welcomes You !!!", fg="red", bg="sky blue"
, font=('Arial Black', 20), width=22, anchor=CENTER)
lb2 = Label(root, text="What would you like to compare?",
font=('Arial', 18), anchor=CENTER)
space1 = Label(root, text="\n\n")
lb1.grid(row=0)
lb2.grid(row=5)
space1.grid(row=1)
hpw, mil = StringVar(), StringVar()
def bt_cars():
w1 = Toplevel()
w1.title("Choose Features")
w1.geometry("430x200")
lb3 = Label(w1, text="Choose features for comparison", bg="yellow"
, font=('Arial Black', 18), width=25)
lb4 = Label(w1, text=" ", anchor=CENTER)
fr1 = LabelFrame(w1, width=20, padx=100)
hpw_cb = Checkbutton(fr1, text="Horsepower", variable=hpw, anchor='w', onvalue="Horsepower", offvalue="")
hpw_cb.grid()
hpw_cb.deselect()
mil_cb = Checkbutton(fr1, text="Mileage", variable=mil, anchor='w', onvalue="Mileage", offvalue="")
mil_cb.grid()
mil_cb.deselect()
var_stor = [hpw, mil]
print(hpw)
print(mil)
var_fill = []
for itr1 in var_stor:
if itr1 != "":
var_fill.append(itr1)
print(var_fill)
def car_1():
name1 = StringVar()
c1 = Toplevel()
c1.title("Car 1")
c1.geometry("430x200")
car1_lb1 = Label(c1, text="Car Name:")
name1_ifl = Entry(c1)
name1 = name1_ifl.get()
elm_var_fill = len(var_fill)
ct1 = 0
car1_val = []
for itr2 in var_fill:
if ct1 == elm_var_fill:
break
lb5 = Label(c1, text=itr2.get())
#Creating text field
ftr1_ifl = Entry(c1)
car1_ftr = ftr1_ifl.get()
car1_val.append(car1_ftr)
car1_ftr = None
lb5.grid(row=ct1 + 2, column=1)
ftr1_ifl.grid(row=ct1 + 2, column=2)
ct1 += 1
print(car1_val)
def display():
dp = Toplevel()
dp.title("Results")
dp.geometry("500x200")
car1_pt = 0
car2_pt = 0
car_tree = tktrv.Treeview(dp)
car_tree["columns"] = ("car1col")
car_tree.column("#0", width=120, minwidth=30)
car_tree.column("car1col", width=120, minwidth=30)
car_tree.heading("#0", text="Features" )
car_tree.heading("car1col", text=str(name1))
car_tree.pack()
c1.withdraw()
print(var_fill)
done1_bt = Button(c1, text="Continue", command=display)
name1_ifl.grid(row=0, column=2)
car1_lb1.grid(row=0, column=1)
done1_bt.grid(row=5,column=1)
w1.withdraw()
done_bt = Button(w1, text="Done", command=car_1)
done_bt.grid(row=3, column=1)
lb3.grid(row=0, column=1)
lb4.grid(row=1, column=1)
fr1.grid(row=2, column=1)
root.withdraw()
bt1 = Button(root, text="CARS", width=5, font=('Calibri', 15), command=bt_cars)
bt1.grid(row=7)
space2 = Label(root, text="\n\n")
space2.grid(row=6)
root.mainloop()
I am facing trouble with the variables named: hpw, mil, name1.
Any help would be welcome.
NOTE:- Please excuse the amount of code; I wanted others to replicate the error and see it for themselves
For the variables hpw and mil, these variables are empty strings that's why you are not getting any value from those checkboxes. To get values from the checkboxes replace these lines of code:
var_stor = [hpw, mil]
with
var_stor = [hpw_cb.cget('onvalue'), mil_cb.cget('onvalue')]
since you want the onvalue then you must use cget() method to access those values.
also, replace
lb5 = Label(c1, text=itr2.get())
with
lb5 = Label(c1, text=itr2)
because now you have required values (not objects) in a list, so just need to access those values.
For the variable name1 you can use #BokiX's method.
The problem is you are using get() wrong. You cannot use get() right after Entry() because as soon as entry is created it's getting the input before the user can even input something.
Use this code:
def get_input(text):
print(text)
e = Entry(root)
e.pack()
b = Button(root, text="Print input", command=lambda: get_input(e.get()))
b.pack()
Now get() method will not be executed before you click the button.

Function to generate random string with nested function is giving me trouble

I'm gonna preface this by saying that im quite new to this, dont judge haha.
The script im writing is to make a random password from different strings and randomize them. That I have already succeeded in completing, but know that I want to build a GUI for that script, the implementation with functions and tkinter is giving me trouble. I have two functions: One (add2all)that is supposed to check wether a variable is true, and if it is true add it to the string "all", that im later going to randomize with my second function. Im sure there are problems with how I get the value from the Checkbuttons (used to get the user input what kind of characters they want in the password), and then how they are added to the string. My second function (pwdgen) is supposed to get the input length from an Entry box were the user types an int, and then calls the add2all function to see what characters the user wants. add2all should generate a string with all the charracters that have been defined as true, so that pwdgen can randomize them with the given length. I also have a Button that is supposed to start the process. It calls on pwdgen, which then ccalls add2all and at the end, the variable password should have a randomized string which I can display to an entry box.
TL;DR: The function and tkinter implementation of a very simple script I previously wrote isn't working at all.
import random
from tkinter import *
from tkinter import ttk
root = Tk()
upper, lower, nums, syms = False, False, False, False
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lowercase = "abcdefghijklmnopqrstuvwxyz"
digits = "1234567890"
symbols = "!§$%&?*+#~-;()/"
all = StringVar()
CheckBool1 = IntVar()
CheckBool2 = IntVar()
CheckBool3 = IntVar()
CheckBool4 = IntVar()
pwlength = IntVar()
#function that checks if a CheckButton input (which characters are wanted in password)
#is true, if true adds the corresponding character string to the all string
def add2all():
if CheckBool1:
upper = True
if CheckBool2:
lower = True
if CheckBool3:
nums = True
if CheckBool4:
syms = True
if upper:
all += uppercase
if lower:
all += lowercase
if nums:
all += digits
if syms:
all += symbols
#function that gets password length from user, then calls add2all, then gives var password
#the string of all randomized with specified length and then puts it into entry box pwdoupt
def pwdgen():
pwdoutpt.delete(0, END)
v = amt.get()
v = pwlength
add2all()
password = "".join(random.choices(all, k=pwlength))
pwdoutpt.insert(0, password)
greet = Label(root, text="Welcome to the interactive password generator!", height=3)
pwdoutpt = Entry(root, width=30)
generate = Button(root, text="Generate", command = lambda: pwdgen())
#inits all the labels and checkbuttons for my gui
chck_1 = Checkbutton(root, width = 5, var = CheckBool1)
chck_2 = Checkbutton(root, width = 5, var = CheckBool2)
chck_3 = Checkbutton(root, width = 5, var = CheckBool3)
chck_4 = Checkbutton(root, width = 5, var = CheckBool4)
lbl_1 = Label(root, text="Capital Letters")
lbl_2 = Label(root, text="Lowercase Letters")
lbl_3 = Label(root, text="Digits")
lbl_4 = Label(root, text="Symbols")
amt = Entry(root, width = 5)
amtlbl = Label(root, text="Num of characters in pwd.")
sep = ttk.Separator(root, orient="horizontal")
greet.grid(column=0, row=0, columnspan=4, pady=3)
pwdoutpt.grid(column=0, row=7, columnspan=4, padx=30, sticky=W)
amt.grid(column=0, row=1, sticky=W)
amtlbl.grid(column=1, row=1, columnspan=1, sticky=E)
chck_1.grid(column=0, row=2, sticky=E)
chck_2.grid(column=0, row=3, sticky=E)
chck_3.grid(column=0, row=4, sticky=E)
chck_4.grid(column=0, row=5, sticky=E)
lbl_1.grid(column=1, row=2, columnspan=1, sticky=W)
lbl_2.grid(column=1, row=3, columnspan=1, sticky=W)
lbl_3.grid(column=1, row=4, columnspan=1, sticky=W)
lbl_4.grid(column=1, row=5, columnspan=1, sticky=W)
sep.grid(column=0, columnspan=10, row=6, sticky=EW)
generate.grid(column=2, row=7)
root.mainloop()
Firstly, don't use all as the variable name for your StringVar; it's a built-in python function. Don't use wildcard imports (from <module> import *), it's a bad habit and doesn't conform to PEP8. Instead, use import tkinter as tk.
You need to read how to use a StringVar; it is NOT a string and you can't add to it like one. A good application of using a StringVar in this code would be to display the generated password. If you bind the StringVar to your pwdoutpt Entry, updating the StringVar will automatically display the updated string.
You say you already had your password generator working correctly. Why not slightly adapt your password generator function so it takes the length and character types as inputs, and returns a randomly generated password? You are currently trying to integrate the password generating function into the GUI code, which is a bad idea is it can get messy super fast.
Taking all of the above into consideration, here is a working modified version of your code:
import random
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
# Your password generating function
def pwdgen(password_length, chars_allowed):
chars = [
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"abcdefghijklmnopqrstuvwxyz",
"1234567890",
"!§$%&?*+#~-;()/"
]
valid_chars = [x for i, x in enumerate(chars) if chars_allowed[i] == True]
valid_chars = "".join(valid_chars)
password = "".join(random.choices(valid_chars, k=password_length))
return password
# Set up tkinter window
root = tk.Tk()
root.title("Password generator")
# Vars for checkbuttons and password entry
CheckBool1 = tk.IntVar()
CheckBool2 = tk.IntVar()
CheckBool3 = tk.IntVar()
CheckBool4 = tk.IntVar()
password = tk.StringVar()
# Main widgets
greet = tk.Label(root, text="Welcome to the interactive password generator!", height=3)
chck_1 = tk.Checkbutton(root, width = 5, var = CheckBool1)
chck_2 = tk.Checkbutton(root, width = 5, var = CheckBool2)
chck_3 = tk.Checkbutton(root, width = 5, var = CheckBool3)
chck_4 = tk.Checkbutton(root, width = 5, var = CheckBool4)
lbl_1 = tk.Label(root, text="Capital Letters")
lbl_2 = tk.Label(root, text="Lowercase Letters")
lbl_3 = tk.Label(root, text="Digits")
lbl_4 = tk.Label(root, text="Symbols")
amt = tk.Entry(root, width = 5)
amtlbl = tk.Label(root, text="Num of characters in pwd.")
sep = ttk.Separator(root, orient="horizontal")
# Password output
pwdoutpt = tk.Entry(root, width=30, textvariable=password, state="readonly")
# Generate password function and button
def insert_password():
try:
password_length = int(amt.get())
chars_allowed = [CheckBool1.get(), CheckBool2.get(), CheckBool3.get(), CheckBool4.get()]
password.set(pwdgen(password_length, chars_allowed))
except ValueError:
messagebox.showerror("Error", "Invalid password length")
except IndexError:
messagebox.showerror("Error", "No character type selected")
generate = tk.Button(root, text="Generate", command = insert_password)
# Gridding widgets
greet.grid(column=0, row=0, columnspan=4, pady=3)
pwdoutpt.grid(column=0, row=7, columnspan=4, padx=30, sticky=tk.W)
amt.grid(column=0, row=1, sticky=tk.W)
amtlbl.grid(column=1, row=1, columnspan=1, sticky=tk.E)
chck_1.grid(column=0, row=2, sticky=tk.E)
chck_2.grid(column=0, row=3, sticky=tk.E)
chck_3.grid(column=0, row=4, sticky=tk.E)
chck_4.grid(column=0, row=5, sticky=tk.E)
lbl_1.grid(column=1, row=2, columnspan=1, sticky=tk.W)
lbl_2.grid(column=1, row=3, columnspan=1, sticky=tk.W)
lbl_3.grid(column=1, row=4, columnspan=1, sticky=tk.W)
lbl_4.grid(column=1, row=5, columnspan=1, sticky=tk.W)
sep.grid(column=0, columnspan=10, row=6, sticky=tk.EW)
generate.grid(column=2, row=7)
root.mainloop()
Some further notes on the code for you:
The function insert_password uses a try except to test if the inputs are correct before generating the password.
pwdoutpt has state="readonly" so that the user can't modify the generated password but can still copy it

Python Tkinter GUI messy printing format

import pandas as pd
import csv
from tkinter import *
master = Tk()
textBox = Text(master, height=1, width=10)
textBox.grid(row=0, column=1)
fileVar = StringVar()
fileLabel = Label(master, textvariable=fileVar)
fileLabel.grid(row=3, column=1)
fileVar2 = StringVar()
fileLabel2 = Label(master, textvariable=fileVar)
fileLabel2.grid(row=3, column=2)
def retrieve_input():
Customer = textBox.get("1.0","end-1c")
fileread = pd.read_csv('50.csv', encoding='latin-1')
filevalue = fileread.loc[fileread['Customer'].str.contains(Customer, na=False),"Jul-18\nQty"]
productheader = fileread.loc[fileread['Customer'].str.contains(Customer, na=False), 'Product']
fileVar.set(productheader)
fileVar2.set(filevalue)
button1 = Button(master,text="Show Values", command=lambda: retrieve_input())
button1.grid(row=4, column=1)
mainloop()
I have this program that prints values in Tkinter Gui, for specific values in a specific row. However the format of the values being printed is quiet messy and I am unsure how to clean it up, In addition I would like for it to not display the numbers labeled next to it, Just the product name (blurred out in blue) and the value associated with it( which are the number values with decimals).
Here is a snapshot of the GUI:
In addition, at the very bottom of the list, this is also displayed:
Ideally, I would like for it to only show the header(July-18) and not the datatype
Also if it helps to know, these are forecast files, (reason for headers with dates)
Here is a dummy image of what my csv files look like:
Ok so after some testing I think I have found a fix that should work for you.
The main issue with spacing inside of your label is the problem with your font not being one that is considered monospace.
Try using the font Consolas and see how well that fixes your layout.
import pandas as pd
from tkinter import *
master = Tk()
textBox = Text(master, height=1, width=10, font=('Consolas', 12))
textBox.grid(row=0, column=1)
fileVar = StringVar()
fileLabel = Label(master, textvariable=fileVar, font=('Consolas', 12))
fileLabel.grid(row=3, column=1)
def retrieve_input():
department = textBox.get("1.0","end-1c")
fileread = pd.read_csv('50.csv', encoding='latin-1')
filevalue = fileread.loc[fileread['Customer'].str.contains(department, na=False),("Jul-18\nQty", "Product")]
fileVar.set(filevalue)
button1 = Button(master,text="Show Values", command=lambda: retrieve_input())
button1.grid(row=4, column=1)
master.mainloop()

Python3.6 : Trying to avoid using a global for my swap function with tkinter

I have only been programming 3 months so any advice on improvement to my code is appreciated even if it inst related to my specific question.
Its a simple small project with tkinter. Two fields to enter your first and last name then you hit the swap button and it will swap what ever you put in the name fields.
Problem is I dont want to use globals and I cant seem to figure it out I know its probably something easy and I did spend time trying to figure it out.
If you have any improvements to the code let me know.
from tkinter import *
### I dont Want Globals but cant figure out another method for doing this
### Hope some one can help me with this part
evar = ""
evar1 = ""
def mainWindow():
root = Tk()
root.title("Swap Names")
root.geometry("400x150+100+250")
return root
def createVar():
global evar
global evar1
evar = StringVar()
evar1 = StringVar()
def firstNameFrame(root):
frame1 = Frame(root)
frame1.pack(side=TOP, padx=2, pady=2)
label = Label(frame1, text="First Name:")
label.pack(side=LEFT, padx=2, pady=2)
entry = Entry(frame1, textvariable = evar)
entry.pack(side=LEFT, pady = 2)
def lastNameFrame(root):
frame2 = Frame(root)
frame2.pack(side=TOP, padx=2, pady=2)
label = Label(frame2, text="Last Name:")
label.pack(side=LEFT, padx=1, pady=1)
entry = Entry(frame2, textvariable = evar1)
entry.pack(side=LEFT, pady = 5)
def swapFrame(root):
frame3 = Frame(root)
frame3.pack(side=TOP, padx=10, pady = 10)
swapButton = Button(frame3, text="Swap",command = swap)
swapButton.pack(side=LEFT, padx =5, pady=5)
### I would like to some how use swap with out using a global
def swap():
b=evar.get()
evar.set(evar1.get())
evar1.set(b)
def main():
root = mainWindow()
createVar()
firstNameFrame(root)
lastNameFrame(root)
swapFrame(root)
root.mainloop()
main()
One of the solutions can be wrapping all the code related to the initialization and working with Tk in a separate class, so instead of global variables, we will use the class instance variables:
from tkinter import *
class Gui(object):
def __init__(self):
self.root = Gui._init_main_window()
self.first_name_var = StringVar()
self.last_name_var = StringVar()
self._init_first_name_frame()
self._init_last_name_frame()
self._init_swap_frame()
#staticmethod
def _init_main_window():
root = Tk()
root.title("Swap Names")
root.geometry("400x150+100+250")
return root
def _init_first_name_frame(self):
frame1 = Frame(self.root)
frame1.pack(side=TOP, padx=2, pady=2)
label = Label(frame1, text="First Name:")
label.pack(side=LEFT, padx=2, pady=2)
entry = Entry(frame1, textvariable=self.first_name_var)
entry.pack(side=LEFT, pady=2)
def _init_last_name_frame(self):
frame2 = Frame(self.root)
frame2.pack(side=TOP, padx=2, pady=2)
label = Label(frame2, text="Last Name:")
label.pack(side=LEFT, padx=1, pady=1)
entry = Entry(frame2, textvariable=self.last_name_var)
entry.pack(side=LEFT, pady=5)
def _init_swap_frame(self):
frame3 = Frame(self.root)
frame3.pack(side=TOP, padx=10, pady=10)
swap_button = Button(frame3, text="Swap", command=self._swap)
swap_button.pack(side=LEFT, padx=5, pady=5)
def _swap(self):
tmp = self.first_name_var.get()
self.first_name_var.set(self.last_name_var.get())
self.last_name_var.set(tmp)
def mainloop(self):
return self.root.mainloop()
def main():
gui = Gui()
gui.mainloop()
if __name__ == '__main__':
main()
A small comment to the code above: adding a prefix __ to variables or methods allows you to hide access to them directly by name outside the class using the name mangling.
UPD: According to #Coal comment, changed the double underscore prefixes to single underscore, as there is no need to use a name mangling.
This is assuming that when you say you don't want to use global, that you also mean that you don't want to use self:
from tkinter import Tk, Button, Entry
def swap(fn, ln):
# Get the contents of the two fields.
first = fn.get()
last = ln.get()
# Clear the contents of both fields.
first_name.delete(0, 'end')
last_name.delete(0, 'end')
# Set each field to the previous content of the other field.
first_name.insert(0, last)
last_name.insert(0, first)
root = Tk()
first_name = Entry(root)
last_name = Entry(root)
first_name.insert(0, "Enter first name")
last_name.insert(0, "Enter last name")
first_name.pack()
last_name.pack()
swap_button = Button(root, text="SWAP", command=lambda:swap(first_name, last_name))
swap_button.pack()
root.mainloop()

Categories

Resources