Tried using textvariable and insert but it didn't work. What I hope to achieve is this:
But the text wont be stored as a value inside the combobox just a default text when you haven't selected anything yet.
This is my code so far:
root = Tk()
root.geometry('800x600')
root.title('CSSD')
topFrame=Frame(root,width=800,height=150,pady=10,padx=250)
area=Label(topFrame,text='CSSD')
area.config(font=("Courier", 100))
frame=Frame(root,highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, bd= 0)
frame.place(relx=.5, rely=.5, anchor="center")
username = Label(frame, text='User Name')
username.config(font='Arial',width=15)
password = Label(frame, text='Password')
password.config(font='Arial',width=15)
enteruser = Entry(frame, textvariable=StringVar(),font=large_font)
enterpass = Entry(frame, show='*', textvariable=StringVar(),font=large_font)
combo=ttk.Combobox(frame)
combo['values']=('Clinic 1','Clinic 2','Clinic 3','Clinic 4','Clinic 5','Clinic 6','Clinic 10','Clinic 11','OB')
combo.state(['readonly'])
combo.grid(row=0,sticky=NW)
topFrame.grid(row=0,sticky=EW)
area.grid(row=0,columnspan=300)
username.grid(row=1, sticky=E)
enteruser.grid(row=1, column=1)
password.grid(row=2, sticky=E)
enterpass.grid(row=2, column=1)
You can use the set method like so:
combo.set('default')
Just another option in case nobody mentioned it.
method = StringVar(value='default')
choosen = ttk.Combobox(root, width = 14, textvariable = method, )
And this is working in Checkbutton too.
Related
I am creating a password manager which includes some buttons, but for some reason these buttons aren't aligning properly, could someone help out?
Here is the code i've done usint Tkinter for these buttons:
btn = Button(window, text="Exit Securely", command=exit)
btn.grid(column=2)
btn = Button(window, text="Add Entry", command=addEntry)
btn.grid(column=1)
btn = Button(window, text="Generate", command=run)
btn.grid(column=0)
lbl = Label(window, text="Website")
lbl.grid(row=3, column=0, padx=80)
lbl = Label(window, text="Username")
lbl.grid(row=3, column=1, padx=80)
lbl = Label(window, text="password")
lbl.grid(row=3, column=2, padx=80)
which makes my program look like this:
Any general tips or helpful links for how to make a nicer GUI would be appreciated as well, as I have been struggling with that.
As #acw1668 said if you don't specify row in grid(), it will take the next available row.
# Code to make this example work:
from tkinter import *
def addEntry():pass
def run():pass
window = Tk()
# Added `row=0` for each one of them
btn = Button(window, text="Exit Securely", command=exit)
btn.grid(row=0, column=2)
btn = Button(window, text="Add Entry", command=addEntry)
btn.grid(row=0, column=1)
btn = Button(window, text="Generate", command=run)
btn.grid(row=0, column=0)
# Changed the row to 1 for all of them
lbl = Label(window, text="Website")
lbl.grid(row=1, column=0, padx=80)
lbl = Label(window, text="Username")
lbl.grid(row=1, column=1, padx=80)
lbl = Label(window, text="password")
lbl.grid(row=1, column=2, padx=80)
By the way it is a good idea to use different names for the different buttons/labels.
I have been trying various method of aligning the widgets of tkinter in the program window lately and well I have found a better working solution to this.
In you program you have been using grid for aligning. I would say that you replace with place instead.
place will allow you to set a definite x and y coordinate for the widget and it would be easy to use.
If I alter your code accordingly, I can show you the code (after alteration) and the image of the output.
Code (After Alteration)
# Code to make this example work:
from tkinter import *
def addEntry():pass
def run():pass
window = Tk()
# Adding geometry ettig.
window.geometry('500x500')
btn = Button(window, text="Exit Securely", command=exit)
btn.place(x=410, y=20)
btn = Button(window, text="Add Entry", command=addEntry)
btn.place(x=210, y=20)
btn = Button(window, text="Generate", command=run)
btn.place(x=10, y=20)
lbl = Label(window, text="Website")
lbl.place(x=10, y=50)
lbl = Label(window, text="Username")
lbl.place(x=210, y=50)
lbl = Label(window, text="password")
lbl.place(x=410, y=50)
The Output Screen
I am making a program that searches a list of stock numbers(the first 8 digit int in the list) and then returns the rest of the values in the list inside the entry boxes but when I click the search button, it does not perform its function. Any advice? (I'm also new to coding, is there any place where I could've shortened my code anywhere to make it more efficient?)
root = Tk()
#lists
car = [19225735, '611926', '2018', 'Hyundai', 'Sonata', 'White', 'Recon', '$25,000',
'Sedan',32,123]
#funtion
def search():
x = stockNumber.get()
if (x == car[0]):
vinNumber.insert(car[1])
make.insert(car[3])
model.insert(car[4])
color.insert(car[5])
status.inset(car[6])
price.insert(car[7])
size.insert(car[8])
mileage.insert(car[9])
#text boxes --------------------------------------------
stockNumber = Entry(root, width=30)
stockNumber.grid(row=0, column=1, padx=20)
vinNumber = Entry(root, width=30)
vinNumber.grid(row=1, column=1, padx=20
year = Entry(root, width=30)
year.grid(row=2, column=1, padx=20)
make = Entry(root, width=30)
make.grid(row=3, column=1, padx=20)
model = Entry(root, width=30)
model.grid(row=4, column=1, padx=20)
color = Entry(root, width=30)
color.grid(row=5, column=1, padx=20)
status = Entry(root, width=30)
status.grid(row=6, column=1, padx=20)
price = Entry(root, width=30)
price.grid(row=7, column=1, padx=20)
size = Entry(root, width=30)
size.grid(row=8, column=1, padx=20)
mileage = Entry(root, width=30)
mileage.grid(row=8, column=1, padx=20)
#button command-------------------------------
enter = Button(root, text = "Search", padx=40, pady=20, command=search)
enter.grid(row=9, column=0)
#labels ------------------------------------------------
snLabel = Label(root, text="Stock Number")
snLabel.grid(row=0, column=0)
vnLabel = Label(root, text="Vin Number")
vnLabel.grid(row=1, column=0)
yearLabel = Label(root, text="Year")
yearLabel.grid(row=2, column=0)
makeLabel = Label(root, text="Make")
makeLabel.grid(row=3, column=0)
modelLabel = Label(root, text="Model")
modelLabel.grid(row=4, column=0)
colorLabel = Label(root, text="Color")
colorLabel.grid(row=5, column=0)
statusLabel = Label(root, text="Status")
statusLabel.grid(row=6, column=0)
sizeLabel = Label(root, text="Size")
sizeLabel.grid(row=7, column=0)
mileLabel = Label(root, text="Mileage")
mileLabel.grid(row=8, column=0)
The Button was working fine, your function was the problem:
def search():
x = int(stockNumber.get())
if (x == car[0]):
vinNumber.insert(0,str(car[1]))
make.insert(0,str(car[3]))
model.insert(0,str(car[4]))
color.insert(0,str(car[5]))
status.insert(0,str(car[6]))
price.insert(0,str(car[7]))
size.insert(0,str(car[8]))
mileage.insert(0,str(car[9]))
This is what i fixed:
stockNumber.get() returns a string, you compare it with an integer, if you do that, it will always be false -> convert it to int with int()
.insert needs an index aswell, not just insert(data) but insert(index, data)
Here is a hint!!!
You need to take a look at each point of data and understand the kind of data you are wanting your function to pull. Yes, you want a number! But is it an "int", a float, what is it???
Take a look at your function for search(). The only reason why it is not grabbing the data is that you need to make sure the function is looking for the right kind of data. Since "stock number" is probably an integer, you would need to specify that in your function.
Get a basic understanding of Python data types:
int
float
string
I'm just about ready to pull my hair out - have tried just about everything I can imagine to do something that on the face of it seems fairly simple...
I need to have an Entry box, which takes in a variable, which is returned to the code and can be used as a variable throughout the script. I actually need to import this script and use it in the code of another script.
At the moment, I know the Submit button is calling the get_data() function, because using 'print' displays the password entered. But using return, to return it to the parent function, and then returning that value and printing the output of the main function returns nothing.
Thanks
from tkinter import *
def get_params():
def get_data():
pw = pwentry_enter.get()
return pw
window = Tk()
headFrame = Frame(window)
headFrame.grid(row=0, pady=6)
header = Label(headFrame, text="Input Password", font=(f1, 20))
header.grid(column=0, row=0, columnspan=2, sticky="w")
mainFrame = Frame(window, bg="#1B2230")
mainFrame.grid(row=1, pady=6)
raw_password = StringVar()
pwentry_enter=Entry(mainFrame, width=30, font=(f2,10), show="*", textvariable=raw_password)
pwentry_enter.pack()
btnFrame = Frame(window)
btnFrame.grid(row=2, pady=6)
submit_btn = Button(btnFrame, text='Submit', command=get_data, width=10, bg="#DB4158", fg="black", font=(f2, 20))
submit_btn.grid(column=1, row=0)
quit_btn = Button(btnFrame, text='Quit', command=window.destroy, width=10, bg="#DB4158", fg="black", font=(f2, 20))
quit_btn.grid(column=0, row=0)
window.mainloop()
a = get_data()
return a
Don't add return in get_data() and use a global variable to store the password when submit button is clicked and return when quit button is pressed.
You are trying to read data of the Entry after destroying the window.
from tkinter import *
pw = ''
def get_params():
global pw
def get_data():
global pw
pw = pwentry_enter.get()
window = Tk()
headFrame = Frame(window)
headFrame.grid(row=0, pady=6)
header = Label(headFrame, text="Input Password", font=(f1, 20))
header.grid(column=0, row=0, columnspan=2, sticky="w")
mainFrame = Frame(window, bg="#1B2230")
mainFrame.grid(row=1, pady=6)
raw_password = StringVar()
pwentry_enter=Entry(mainFrame, width=30, font=(f2,10), show="*", textvariable=raw_password)
pwentry_enter.pack()
btnFrame = Frame(window)
btnFrame.grid(row=2, pady=6)
submit_btn = Button(btnFrame, text='Submit', command=get_data, width=10, bg="#DB4158", fg="black", font=(f2, 20))
submit_btn.grid(column=1, row=0)
quit_btn = Button(btnFrame, text='Quit', command=window.destroy, width=10, bg="#DB4158", fg="black", font=(f2, 20))
quit_btn.grid(column=0, row=0)
window.mainloop()
return pw
Could you help me understand what I am doing wrongly.
I want this
My code is:
from tkinter import *
import Pmw
master = Tk()
class GetPassword(Pmw.Dialog):
def body(self, master):
self.title('Enter New Password')
Label(master, text='Old Password:').grid(row=0, sticky=W)
Label(master, text='New Password:').grid(row=1, sticky=W)
Label(master, text='Enter New Password Again:').grid(row=2, sticky=W)
self.oldpw = Entry(master, width = 16, show='*')
self.newpw1 = Entry(master, width = 16, show='*')
self.newpw2 = Entry(master, width = 16, show='*')
self.oldpw.grid(row=0, column=1, sticky=W)
self.newpw1.grid(row=1, column=1, sticky=W)
self.newpw2.grid(row=2, column=1, sticky=W)
a = GetPassword()
a.body(master)
master.mainloop()
The result looks likt this:
My problems:
The title is Tk instead of Enter New Password.
I somehow manage to split the frame in two: 1. Entry fields. 2. Buttons.
I'm getting only one button (Cancel button is lost somewhere).
I would be thankful if you could help me.
1. The title is Tk instead of Enter New Password.
You need to set title for master. The code in the question put widgets in the master (main window).
master = Tk()
master.title('Enter New Password')
This may not be needed, if you solve the second problem.
2. I somehow manage to split the frame in two: ...
In the dialog body method, you're putting widget into the master. Use Pmw.Dialog.interior() to put widgets into dialog:
def body(self, master):
self.title('Enter New Password')
Label(self.interior(), text='Old Password:').grid(row=0, sticky=W)
Label(self.interior(), text='New Password:').grid(row=1, sticky=W)
Label(self.interior(), text='Enter New Password Again:').grid(row=2, sticky=W)
self.oldpw = Entry(self.interior(), width = 16, show='*')
self.newpw1 = Entry(self.interior(), width = 16, show='*')
self.newpw2 = Entry(self.interior(), width = 16, show='*')
self.oldpw.grid(row=0, column=1, sticky=W)
self.newpw1.grid(row=1, column=1, sticky=W)
self.newpw2.grid(row=2, column=1, sticky=W)
3. I'm getting only one button (Cancel button is lost somewhere).
Use buttons option to list names of buttons:
a = GetPassword(buttons=('OK', 'Cancel'))
I have been trying to make an entry box for people to enter their names, as part of a "registration" for a mini-program that I am doing, but the result is that an entry box doesn't appear at all.
Here is the section of the code that is troubling me:
def win2(self):
# this is the child window
board = Toplevel()
board.title("Sign up")
board.focus_set()
board.grab_set()
userVar = StringVar()
userVar.set('Username')
square1Label = Label(board,textvariable=userVar)
square1Label.grid(row=0, column=7)
userEnt=Entry(self)
userEnt.grid(row=1, column=7)
s2Var = StringVar()
s2Var.set('First Name')
square2Label = Label(board,textvariable=s2Var)
square2Label.grid(row=1, column=7)
leaveButton = Button(board, text="Quit", command=board.destroy)
leaveButton.grid(row=1, column=1, sticky='nw')
board.wait_window(board)
And here is the coding in full, minus the importing of Tkinter and the mainloop:
class Application(Frame):
"""GUI Application for making Baakibook"""
def __init__(self, parent):
"""Initialize the frame"""
Frame.__init__(self, parent, bg="light blue")
self.win1()
# different windows
def win1(self):
# this is the main/root window
signupButton = Button(root, text="Sign up", command=self.win2)
signupButton.grid(row=9, column=7)
loginButton = Button(root, text="Log in", command=self.win3)
loginButton.grid(row=10, column=7)
leaveButton = Button(root, text="Quit", command=root.destroy)
leaveButton.grid(row=1, column=1, sticky='nw')
b1Var = StringVar()
b2Var = StringVar()
b1Var.set('b1')
b2Var.set('b2')
box1Label = Label(root,textvariable=b1Var,width=12)
box1Label.grid(row=3, column=2)
box2Label = Label(root,textvariable=b2Var,width=12)
box2Label.grid(row=3, column=3)
root.mainloop()
def win2(self):
# this is the child window
board = Toplevel()
board.title("Sign up")
board.focus_set()
board.grab_set()
userVar = StringVar()
userVar.set('Username')
square1Label = Label(board,textvariable=userVar)
square1Label.grid(row=0, column=7)
userEnt=Entry(self)
userEnt.grid(row=1, column=7)
s2Var = StringVar()
s2Var.set('First Name')
square2Label = Label(board,textvariable=s2Var)
square2Label.grid(row=1, column=7)
leaveButton = Button(board, text="Quit", command=board.destroy)
leaveButton.grid(row=1, column=1, sticky='nw')
board.wait_window(board)
def win3(self):
# this is the child window
board = Toplevel()
board.title("Login User")
board.focus_set()
board.grab_set()
s1Var = StringVar()
s2Var = StringVar()
s1Var.set("s1")
s2Var.set("s2")
square1Label = Label(board,textvariable=s1Var)
square1Label.grid(row=0, column=7)
square2Label = Label(board,textvariable=s2Var)
square2Label.grid(row=0, column=6)
leaveButton = Button(board, text="Quit", command=board.destroy)
leaveButton.grid(row=1, column=1, sticky='nw')
board.wait_window(board)
The first argument you use to create the widget is what parent you want it assigned to. So, this line:
userEnt=Entry(self)
userEnt.grid(row=1, column=7)
makes the Entry widget in the Frame that you created in __init__. If you want it to appear in the Toplevel, create it like the other widgets you made in that method, ie:
userEnt=Entry(board)
userEnt.grid(row=1, column=7)
Also, your grid in the Toplevel doesn't make sense right now and a couple of your widgets will appear stacked until you change it.