Tkinter how to lock Text widget by checking checkbox? - python

I am new to programming and Tkinter. I want to DISABLED textbox when checkbox is pressed and open it NORMAL when box is not ticked. Here is my code:
from tkinter import *
root = Tk()
def lock_fields():
if check == True:
data.configure(state=DISABLED)
if check == False:
data.configure(state=NORMAL)
check = BooleanVar()
open_for_edit = Checkbutton(root, text="check the box for editing", variable=check,
onvalue=True, offvalue=False, command=lambda: lock_fields())
open_for_edit.pack()
check = check.get()
data = Text(root)
data.insert(END, "I would like to be able to edit this text only when checkbox is checked.")
data.pack()
root.mainloop()
It seems that for some reason the check-variable is always False when it enters to lock_fields function. I tried passing check argument to the method.

You're pretty close, only thing is that the check.get() line must be in the function. Also you don't need the lambda. Try this:
from tkinter import *
root = Tk()
def lock_fields():
if check.get():
data.configure(state=DISABLED)
else:
data.configure(state=NORMAL)
check = BooleanVar()
open_for_edit = Checkbutton(root, text="check the box for editing", variable=check, onvalue=True, offvalue=False, command=lock_fields)
open_for_edit.pack()
data = Text(root)
data.insert(END, "I would like to be able to edit this text only when checkbox is checked.")
data.pack()
root.mainloop()

Related

Checkbutton does not work on a nested tkinter window

I am trying to create a Tkinter window with a button which when clicked will provide with a new window. The new window has a checkbox and I want some actions to be done based on the checkbox value.
from tkinter import *
from tkinter import messagebox
def my_command():
def run():
pass
def cb_command():
f1 = fname1.get()
messagebox.showinfo("First Name", f1)
if cbVar.get() == 1:
messagebox.showinfo(cbVar.get())
my_button['state'] = 'active'
else:
messagebox.showinfo("Not found!")
my_button['state'] = 'disabled'
root = Tk()
root.geometry("200x200")
fname = Label(root, text="First Name")
fname.grid(row= 0, column = 0, sticky = "news", padx=5, pady=5)
fname1 = Entry(root, width = 10)
fname1.grid(row =0, column = 1, sticky = "news", padx=5, pady=5)
cbVar = IntVar()
cb1 = Checkbutton(root, text="Please check this", variable=cbVar, onvalue=1, offvalue=0, command=cb_command)
cb1.grid(row = 1, column = 0)
my_button = Button(root, text = "Run", bg = '#333333', fg='#ffffff', font = 'Helvetica', command = run, state='disable')
my_button.grid(row = 2, column = 0)
root.mainloop()
window = Tk()
window.geometry("200x200")
button1 = Button(window, text = "Run", command = my_command)
button1.pack()
window.mainloop()
I wrote this simple code which works fine with all other entry widgets. However, the checkbutton in the new window does not work. Can someone suggest any alternative?
Update:
Sorry, that I didn't clarify what actions to be done. I want the checkbox when clicked impact the state of the "Run" button in the toplevel window. The actual actions are based on the "Run" button.
Thank you Thingamabobs for suggesting a very simple solution. Just replaced one instance of Tk with Toplevel and it works.
from tkinter import *
def new_window():
second_window = Toplevel()
def checkbutton_checked():
# If you just want to take some action, once the checkbutton has been checked, you could do this here
# Alternatively you could also add a button to the toplevel and on click check the value of
# the checkbutton and perform actions based on that...
cb1.configure(text="Checkbutton checked")
cb1 = Checkbutton(second_window, text="Check here", command=checkbutton_checked)
cb1.pack()
window = Tk()
b1 = Button(window, text="Open new window", command=new_window)
b1.pack()
window.mainloop()
I hope this provides some help and you can solve your problem, if not let me know please.
Further details about the purpose of the checkbutton would also help me.

How to set the text in tkinter?

I want to set the answer using the set() method without writing it in the text attribute in the Label and also I want to change the message without displaying the messages over each other.
I want the new message to appear and the one before it to disappear. I tried to use the set() and get() methods but it used to return its default value without using the value in the set() method and I don't know why.
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk,Image
root= Tk()
root.title("Project")
root.geometry('500x600')
root.title('Choose Answer')
var = IntVar()
textt = StringVar()
print(textt.set('Clicked..........'))
def Result():
print(textt.set('You Clicked....................'))
if(var.get() == 1):
print(var.get())
print(textt)
textt.set('You Clicked')
resultText = Label(root, text=textt , fg='white',bg='gray', width=40)
resultText.place(x=100,y=200)
else:
textt.set('You did\'t Click')
resultText = Label(root, text=textt, fg='white',bg='grey', width=40)
resultText.place(x=100,y=200)
checkBox = Checkbutton(root, text='Did you Study Math', variable=var, bg='grey')
checkBox.place(x=50,y=50)
button = Button(root, text='Select', fg='white', bg='grey',font='sans-serif',command=Result)
button.place(x=50,y=100)
root.mainloop()
This should work:
from tkinter import *
root = Tk()
root.title("Project")
root.geometry('500x600')
root.title('Choose Answer')
var = IntVar()
textt = StringVar()
resultText = Label(root, textvariable=textt , fg='white',bg='gray', width=40)
resultText.place(x=100,y=200)
def Result():
print(var.get())
if var.get() == 1:
textt.set('You Clicked')
else:
textt.set('You didn\'t Click')
checkBox = Checkbutton(root, text='Did you Study Math', variable=var, bg='grey')
checkBox.place(x=50,y=50)
button = Button(root, text='Select', fg='white', bg='grey',font='sans-serif',command=Result)
button.place(x=50,y=100)
root.mainloop()
When you create resultText, instead of setting the text argument to textt, you need to set the textvariable argument to text. That way, the text of the variable is automatically changed when you change textt.
I removed all the lines where resultText was recreated and re-placed, and just created it once before defining Result(). Also, I removed from tkinter import messagebox, because you already import it when you call from tkinter import *. Just a note: wildcard imports are discouraged, so try to avoid using them in the future.

I get empty string when i use entry in tkinter

I'm trying to add a file path to my application using an entry, but every time the entry returns an empty string
def command1(event):
if entry1.get() == 'localhost' and entry2.get() == 'amine' and entry3.get()=='aze123qsd' and entry4.get()=='GTFS':
top = Toplevel()
top.title("Add a file")
top.geometry("600x300")
text=Label(top ,text="Add the file path")
text.place(relx=0.3 ,rely=0.3)
entry=Entry(top)
entry.place(relx=0.5 ,rely=0.3)
paths=entry.get()
entry.bind('<Return>', commnad3)
button1 = Button(top, text="show the last window")
button1.place(relx=0.55 ,rely=0.7)
button2 =Button(top ,text="Use old data", command=old)
button2.place(relx=0.3 ,rely=0.7)
Your problem is due to creating entry at the same time you are performing get() on that entry. All you will have at that moment is an empty entry field.
You need to grab that entry information after something has been put into it.
Take this example and let me know if you have any questions.
import tkinter as tk
root = tk.Tk()
def print_entry(e):
print(e.get())
def command1():
top = tk.Toplevel()
entry = tk.Entry(top)
entry.pack()
tk.Button(top, text="Print Entry", command=lambda: print_entry(entry)).pack()
tk.Button(root, text="TOPLEVEL", command=command1).pack()
root.mainloop()

How to change the visibility of a button in tkinter?

I'm new to python and I'm trying to create an application in which I want a button to be visible only after I click the "show" button. The button should not be visible from the start of application it should only be visible after clicking on "show" button.
I have this code which hides the button after clicking on another button. It changes its text to "show" after hiding the button.
from tkinter import *
root = Tk()
btn1 = Button(root,text="Example")
btn1.visible = True
btn1.place(x=20, y=50)
btn1.pi = btn1.place_info()
btn3 = Button(root, text="click me", command=lambda:plugin())
btn3.place(x=20, y=150)
def plugin():
master = Tk()
def toggle1():
if btn1.visible:
btnToggle1["text"] = "Show Example"
print ("Now you don't")
btn1.place_forget()
else:
btn1.place(btn1.pi)
print ("Now you see it")
btnToggle1["text"] = "Hide Example"
btn1.visible = not btn1.visible
btnToggle1 = Button(master, text="Hide Example", command=toggle1)
btnToggle1.place(x=70, y=150)
master.mainloop()
root.mainloop()
I want the button to show only after I click on the "show" button, not from the start.
At the risk of stating the obvious, if you don't want the button to be visible at startup, don't show the button at startup.
You're explicitly making the button visible with this line of code near the start of the program:
btn1.place(x=20, y=50)
You're then setting btn1.pi by calling place_info, but you don't need to do that. You can directly set btn1.p without first calling .place followed by place_info.
btn1 = Button(root,text="Example")
btn1.visible = False
btn1.pi = {"x": 20, "y": 50}
Notice that I also changed btn1.visible to False. You don't actually need a separate attribute to track if it's visible, tkinter can answer that question with the method winfo_viewable().
Or, you can simply remove the button after calculating btn1.pi:
btn1.place(x=20, y=50)
btn1.visible = False
btn1.pi = btn1.place_info()
btn1.place_forget()
I know this is not for the question I had a hard time finding this. If you are using the GTK library then
ButtonName.set_visible(False) sets it invisible
the below is for tkinter I just tested the below for tkinter:
from tkinter import *
def hide_me(event):
event.widget.pack_forget()
def make_invisible(widget):
widget.pack_forget()
root = Tk()
btn=Button(root, text="Click")
btn.bind('<Button-1>', hide_me)
btn.pack()
btn2=Button(root, text="Click too")
btn2.bind('<Button-1>', hide_me)
btn2.pack()
make_invisible(btn2)
root.mainloop()
the make_invisible(btn2) method sets it invisible however make sure to call it after you pack the button otherwise it will still show up.
here is my resources I used to get to this code
https://www.tutorialspoint.com/how-to-make-a-tkinter-widget-invisible
and https://newbedev.com/in-tkinter-is-there-any-way-to-make-a-widget-not-visible
i hope that this is what you mean:
from tkinter import *
import tkinter
root = Tk()
btn1 = Button(root,text="Example")
btn1.visible = True
btn1.place(x=20, y=50)
btn1.pi = btn1.place_info()
btn3 = Button(root, text="click me", command=lambda:plugin())
btn3.place(x=20, y=150)
def plugin():
master = Tk()
def toggle1():
if btn1.visible:
btnToggle1["text"] = "Show Example"
btnToggle1["state"] = DISABLED
print ("Now you don't")
btn1.place_forget()
else:
btn1.place(btn1.pi)
print ("Now you see it")
btnToggle1["state"] = NORMAL
btnToggle1["text"] = "Hide Example"
btn1.visible = not btn1.visible
btnToggle1 = Button(master, text="Hide Example", command=toggle1)
btnToggle1.place(x=70, y=150)
master.mainloop()
root.mainloop()

Why is Tkinter Entry's get function returning nothing?

I'm trying to use an Entry field to get manual input, and then work with that data.
All sources I've found claim I should use the get() function, but I haven't found a simple working mini example yet, and I can't get it to work.
I hope someone can tel me what I'm doing wrong. Here's a mini file:
from tkinter import *
master = Tk()
Label(master, text="Input: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
content = entry.get()
print(content) # does not work
mainloop()
This gives me an Entry field I can type in, but I can't do anything with the data once it's typed in.
I suspect my code doesn't work because initially, entry is empty. But then how do I access input data once it has been typed in?
It looks like you may be confused as to when commands are run. In your example, you are calling the get method before the GUI has a chance to be displayed on the screen (which happens after you call mainloop.
Try adding a button that calls the get method. This is much easier if you write your application as a class. For example:
import tkinter as tk
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()
def on_button(self):
print(self.entry.get())
app = SampleApp()
app.mainloop()
Run the program, type into the entry widget, then click on the button.
You could also use a StringVar variable, even if it's not strictly necessary:
v = StringVar()
e = Entry(master, textvariable=v)
e.pack()
v.set("a default value")
s = v.get()
For more information, see this page on effbot.org.
A simple example without classes:
from tkinter import *
master = Tk()
# Create this method before you create the entry
def return_entry(en):
"""Gets and prints the content of the entry"""
content = entry.get()
print(content)
Label(master, text="Input: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
# Connect the entry with the return button
entry.bind('<Return>', return_entry)
mainloop()
*
master = Tk()
entryb1 = StringVar
Label(master, text="Input: ").grid(row=0, sticky=W)
Entry(master, textvariable=entryb1).grid(row=1, column=1)
b1 = Button(master, text="continue", command=print_content)
b1.grid(row=2, column=1)
def print_content():
global entryb1
content = entryb1.get()
print(content)
master.mainloop()
What you did wrong was not put it inside a Define function then you hadn't used the .get function with the textvariable you had set.
you need to put a textvariable in it, so you can use set() and get() method :
var=StringVar()
x= Entry (root,textvariable=var)
Most of the answers I found only showed how to do it with tkinter as tk. This was a problem for me as my program was 300 lines long with tons of other labels and buttons, and I would have had to change a lot of it.
Here's a way to do it without importing tkinter as tk or using StringVars. I modified the original mini program by:
making it a class
adding a button and an extra method.
This program opens up a tkinter window with an entry box and an "Enter" button. Clicking the Enter button prints whatever is in the entry box.
from tkinter import *
class mini():
def __init__(self):
master = Tk()
Label(master, text="Input: ").grid(row=0, sticky=W)
Button(master, text='Enter', command=self.get_content).grid(row=1)
self.entry = Entry(master)
self.entry.grid(row=0, column=1)
master.mainloop()
def get_content(self):
content = self.entry.get()
print(content)
m = mini()

Categories

Resources