from tkinter import *
import tkinter.ttk as ttk
root = Tk()
root.title("TEST ")
lab_pro_val4=Label(root)
lab_pro_val4=Label(root, text="Anything input", width=30, height=1 )
lab_pro_val4.pack()
ent_pro_val4 = Entry(root)
ent_pro_val4.pack()
def btncmd4_add():
tru = (ent_pro_val4=='TEST123')
print(tru)
btn4_ppid = Button(root, text="Check ", command= btncmd4_add,bg = "white")
btn4_ppid.pack()
root.mainloop()
I used Tkinter but I had some trouble.
My question : why is it diff from entry to str(same)
I type the 'TEST123' in entry box.
But it's False... Why is it different?
Please let me know.
ent_pro_val4 is Entry widget, you need to get value of said widget, which can be done using .get(), that is replace
tru = (ent_pro_val4=='TEST123')
using
tru = (ent_pro_val4.get()=='TEST123')
Related
I'm running a simple piece of code wherein whenever a value is selected from the combobox, it needs to be printed in the terminal. But whenever I select a value, after pressing the Quit Button, it's not getting printed on the terminal.
Any nudge would be appreciated.
Thank you for the help
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("200x100")
vals = ('A','B','C','CD','E','FG')
current_var= StringVar()
cb= ttk.Combobox(win, textvariable = current_var)
cb['values']= vals
cb['state']= 'readonly'
cb.pack(fill='x',padx= 5, pady=5)
IP = current_var.get()
Button(win, text="Quit", command=win.destroy).pack()
win.mainloop()
print(IP)
If you want the quit button to print the value then change it to something like this.
from tkinter import *
from tkinter import ttk
def get_value():
IP = current_var.get()
print(IP)
win.destroy()
win = Tk()
win.geometry("200x100")
vals = ('A','B','C','CD','E','FG')
current_var= StringVar()
cb= ttk.Combobox(win, textvariable = current_var)
cb['values']= vals
cb['state']= 'readonly'
cb.pack(fill='x',padx= 5, pady=5)
IP = current_var.get()
Button(win, text="Quit", command= get_value).pack()
win.mainloop()
print(IP)
#Rory's answer isn't quite right because the final print(IP) is simply printing a newline (which he probably didn't notice). To fix that the get_value() callback function should declare global IP so IP is no longer a variable local to the function and its value can be accessed outside the function.
The code below illustrates this and also follows the PEP 8 - Style Guide for Python Code guidelines more closely than what's in your question.
import tkinter as tk # PEP 8 advises avoiding `import *`
import tkinter.ttk as ttk
def get_value():
global IP
IP = current_var.get()
win.destroy()
win = tk.Tk()
win.geometry("200x100")
vals = 'A','B','C','CD','E','FG'
current_var = tk.StringVar(win)
cb = ttk.Combobox(win, textvariable=current_var, values=vals, state='readonly')
cb.pack(fill='x', padx= 5, pady=5)
tk.Button(win, text="Quit", command=get_value).pack()
win.mainloop()
print(IP)
You get the value just after cb is created and the value should be empty string because there is no item is selected at that time. You need to get the value after an item is selected.
One of the way is to move the line IP = current_var.get() after win.mainloop():
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("200x100")
vals = ('A','B','C','CD','E','FG')
current_var = StringVar()
cb= ttk.Combobox(win, textvariable=current_var)
cb['values'] = vals
cb['state'] = 'readonly'
cb.pack(fill='x', padx=5, pady=5)
Button(win, text="Quit", command=win.destroy).pack()
win.mainloop()
IP = current_var.get()
print(IP)
if i write some thing on the textfield and press enter key on the keyboard.what i entered on the textfield display messagebox. this point get error if(format(k=event.char(13)))) : if i set the enter key code. i added full code below.
from tkinter import *
root = Tk()
root.geometry("800x800")
global e1
def callback(event):
if(format(k=event.char(13)))):
msg = e1.get()
print(msg)
Label(root, text="Student Name").place(x=140, y=40)
e1 = Entry(root)
e1.place(x=140, y=10)
e1.bind('<Key>',callback)
root.mainloop()
Try this out
from tkinter import *
root = Tk()
root.geometry("800x800")
def callback(event):
msg = e1.get()
print(msg)
Label(root, text="Student Name").place(x=140, y=40)
e1 = Entry(root)
e1.place(x=140, y=10)
e1.bind('<Return>',callback) #<Return> is equivalent to your Enter key
root.mainloop()
When you click on Enter key, on the entry widget, then the function gets called and the output will be printed out. I also removed the global as it makes no sense to use it in outside functions.
Hope it helped you out.
Cheers
I have a row of widgets which contains a ttk.Combobox and I want to change the background colour of the widgets in the row when I tick a Checkbutton at the end of the row. With tkinter it is simple just to use configure but with ttk you have to use a theme which seems to be neither dynamic nor specific to a single widget. Is there a way to achieve this functionality ?
Thankyou.
in response to fhdrsdg's comment. I can't get it working but this code demonstrates it
import Tkinter as tk
import ttk
def skillUsed():
if chkUsedVar.get() == 1:
style.map('TCombobox', background=[('readonly','green')])
style.map('TCombobox', foreground=[('readonly','red')])
else:
style.map('TCombobox', background=[('readonly','white')])
style.map('TCombobox', foreground=[('readonly','black')])
root = tk.Tk()
style = ttk.Style()
cboxVar1 = tk.StringVar()
cboxVar1.set("spam")
cboxVar2 = tk.StringVar()
cboxVar2.set("silly")
chkUsedVar = tk.IntVar()
chk = tk.Checkbutton(root, text='Used', variable=chkUsedVar, command=skillUsed)
chk.grid(row=0, column=2)
combo01 = ttk.Combobox(root, values=['spam', 'eric', 'moose'], textvariable=cboxVar1)
combo01['state'] = 'readonly'
combo01.grid(row=0, column=0)
combo02 = ttk.Combobox(root, values=['parrot', 'silly', 'walk'], textvariable=cboxVar2)
combo02['state'] = 'readonly'
combo02.grid(row=0, column=1)
root.mainloop()
When the tick box is clicked the foreground goes red and when unticked it goes black. The issue is the background never changes (but doesn't error) and the style is applied globally to both comboboxes and I want to apply it to a single box.
I have a workaround which I will use just using tkinter's OptionMenu and everything I can find on the tinterweb implies it can't be done with ttk widgets but that seems a bit of a limit to ttk widgets but I have little to no experience with tkinter or ttk.
the workaround is :-
from Tkinter import *
def skillUsed():
if chkUsedVar.get() == 1:
opt01.configure(bg="#000fff000")
opt01.configure(highlightbackground="#000fff000")
opt01.configure(activebackground="#000fff000")
opt01.configure(highlightcolor="#000fff000")
opt01["menu"].configure(bg="#000fff000")
else:
opt01.configure(bg=orgOptbg)
opt01.configure(highlightbackground=orgOpthighlightbackground)
opt01.configure(activebackground=orgOptactivebackground)
opt01.configure(highlightcolor=orgOpthighlightcolor)
opt01["menu"].configure(bg=orgOptmenu)
root = Tk()
optionList = ('parrot','silly','walk')
varopt01 = StringVar()
varopt01.set(optionList[0])
chkUsedVar = IntVar()
opt01 = OptionMenu(root, varopt01, *optionList)
opt01.grid(row=0, column=0)
orgOptbg = opt01.cget("bg")
orgOpthighlightbackground = opt01.cget("highlightbackground")
orgOptactivebackground = opt01.cget("activebackground")
orgOpthighlightcolor = opt01.cget("highlightcolor")
orgOptmenu = opt01["menu"].cget("bg")
chk = Checkbutton(root, text='Used', variable=chkUsedVar, command=skillUsed)
chk.grid(row=0, column=1)
root.mainloop()
Thankyou
First, if you want to apply the style to a single combobox, give it a name like 'custom.TCombobox' so that it inherits from 'TCombobox' but doesn't change the default combobox style. Then all you have to do is set the style of your combobox to 'custom.TCombobox'.
Secondly, the background was not changing because it's the fieldbackground you want to change.
EDIT: What can be customized in a style depends on the ttk theme being used. For instance, the default Mac and Windows themes don't allow much customization and the fieldbackground color of the combobox cannot be changed. However, the 'alt' and 'clam' themes allow more customization.
Here is an example based on your code:
import tkinter as tk
from tkinter import ttk
def skillUsed():
if chkUsedVar.get() == 1:
style.map('custom.TCombobox', fieldbackground=[('readonly','green')])
style.map('custom.TCombobox', foreground=[('readonly','red')])
else:
style.map('custom.TCombobox', fieldbackground=[('readonly','white')])
style.map('custom.TCombobox', foreground=[('readonly','black')])
root = tk.Tk()
style = ttk.Style()
style.theme_use('alt')
cboxVar1 = tk.StringVar()
cboxVar1.set("spam")
cboxVar2 = tk.StringVar()
cboxVar2.set("silly")
chkUsedVar = tk.IntVar()
chk = tk.Checkbutton(root, text='Used', variable=chkUsedVar, command=skillUsed)
chk.grid(row=0, column=2)
combo01 = ttk.Combobox(root, values=['spam', 'eric', 'moose'], textvariable=cboxVar1)
combo01['state'] = 'readonly'
combo01.grid(row=0, column=0)
combo02 = ttk.Combobox(root, values=['parrot', 'silly', 'walk'], textvariable=cboxVar2, style='custom.TCombobox')
combo02['state'] = 'readonly'
combo02.grid(row=0, column=1)
root.mainloop()
I'm trying to use Tkinter's Entry widget. I can't get it to do something very basic: return the entered value. Does anyone have any idea why such a simple script would not return anything? I've tried tons of combinations and looked at different ideas.
This script runs but does not print the entry:
from Tkinter import *
root = Tk()
E1 = Entry(root)
E1.pack()
entry = E1.get()
root.mainloop()
print "Entered text:", entry
Seems so simple.
Edit
In case anyone else comes across this problem and doesn't understand, here is what ended up working for me. I added a button to the entry window. The button's command closes the window and does the get() function:
from Tkinter import *
def close_window():
global entry
entry = E.get()
root.destroy()
root = Tk()
E = tk.Entry(root)
E.pack(anchor = CENTER)
B = Button(root, text = "OK", command = close_window)
B.pack(anchor = S)
root.mainloop()
And that returned the desired value.
Your first problem is that the call to get in entry = E1.get() happens even before your program starts, so clearly entry will point to some empty string.
Your eventual second problem is that the text would anyhow be printed only after the mainloop finishes, i.e. you close the tkinter application.
If you want to print the contents of your Entry widget while your program is running, you need to schedule a callback. For example, you can listen to the pressing of the <Return> key as follows
import Tkinter as tk
def on_change(e):
print e.widget.get()
root = tk.Tk()
e = tk.Entry(root)
e.pack()
# Calling on_change when you press the return key
e.bind("<Return>", on_change)
root.mainloop()
from tkinter import *
import tkinter as tk
root =tk.Tk()
mystring =tk.StringVar(root)
def getvalue():
print(mystring.get())
e1 = Entry(root,textvariable = mystring,width=100,fg="blue",bd=3,selectbackground='violet').pack()
button1 = tk.Button(root,
text='Submit',
fg='White',
bg= 'dark green',height = 1, width = 10,command=getvalue).pack()
root.mainloop()
How I disable Entry in Tkinter.
def com():
....
entryy=Entry()
entryy.pack()
button=Button(text="Enter!", command=com, font=(24))
button.pack(expand="yes", anchor="center")
As I said How I disable Entry in com function?
Set state to 'disabled'.
For example:
from tkinter import *
root = Tk()
entry = Entry(root, state='disabled')
entry.pack()
root.mainloop()
or
from tkinter import *
root = Tk()
entry = Entry(root)
entry.config(state='disabled') # OR entry['state'] = 'disabled'
entry.pack()
root.mainloop()
See Tkinter.Entry.config
So the com function should read as:
def com():
entry.config(state='disabled')
if we want to change again and again data in entry box we will have to first convert into Normal state after changing data we will convert in to disable state
import tkinter as tk
count = 0
def func(en):
en.configure(state=tk.NORMAL)
global count
count += 1
count=str(count)
en.delete(0, tk.END)
text = str(count)
en.insert(0, text)
en.configure(state=tk.DISABLED)
count=int(count)
root = tk.Tk()
e = tk.Entry(root)
e.pack()
b = tk.Button(root, text='Click', command=lambda: func(e))
b.pack()
root.mainloop()