how to enable a entry by clicking a button in Tkinter? - python

I need to activate many entries when button is clicked
please do not write class based code, modify this code only because i need to change the whole code for the project as i did my whole project without classes
from Tkinter import *
import ttk
x='disabled'
def rakhi():
global x
x='NORMAL'
root = Tk()
frame=Frame(root)
frame.pack()
entry1=Entry(frame,state=x)
entry1.pack()
entry2=Entry(frame,state=x)
entry2.pack()
button1=Button(frame,text="press",command=rakhi)
button1.pack()
mainloop()

You need to use the configure method of each widget:
def rakhi():
entry1.configure(state="normal")
entry2.configure(state="normal")

Related

How to pop up an existing window by using Tkinter python?

I have created a vocabulary journal by using Tkinter, and I am struggling to pop a window that i created already.
Since i am a beginner, I have not really used class everywhere..
project/build/gui.py
def open_new():# button command
print("clicked")
button_1 = Button(
image=button_image_1,
borderwidth=0,
highlightthickness=0,
command=open_new,
relief="flat"
)
I have created another window in another directory
project/build/build2/gui.py
from db import Database
from pathlib import Path
from tkinter import messagebox
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage,Frame
ob=Database('store.db')
def add_values():
if entry_1.get()=='' or entry_2.get()=='':
messagebox.showerror('no value','please enter anu words')
return
ob.insert(entry_1.get(),entry_2.get())
# print(ob.fetch())
messagebox.showinfo("value has been added", "successfully added")
window = Tk()
blah blah
....
window.mainloop()
WhatI want to do is pop up the second module, which is actually a window. I do not think , it is possible to pop up this window by using toplevel(), is it?
So which code should i use to pop up it?

Properly assigning a function with arguments to a button in Tkinter

I have two files:
functions.py
import tkinter as tk
class funcs():
def func1(entry):
entry.delete(0, tk.END)
main.py
import tkinter as tk
import functions as f
root = tk.Tk()
entrybox = tk.Entry(master=root).grid()
button = tk.Button(master=root, command=f.funcs.func1(entrybox)).grid()
root.mainloop()
In main.py, I have assigned the command func1 with the argument entrybox to the widget button.
My intent is to have the entry argument represent an entry widget I want to manipulate.
This line of code is broken:
button = tk.Button(master=root, command=f.funcs.func1(entrybox)).grid()
The problem is that when I run the program, the function is called immediately and does not get assigned to the button.
I am looking for a way to assign a function with arguments to a button in tkinter.
You can use an anonymous function:
tk.Button(
master=root,
command=lambda: f.funcs.func1(entrybox)
)
Python recognizes the broken line of code as an immediate call, so you have to do lambda:
button = tk.Button(master=root, command=lambda: f.funcs.func1(entrybox)).grid()
and as far as I know that will get rid of the problem.

how to make modern button in tkinter

I want to have abutton like this in tkinter (Modern window 10 buttons):
However, I get this button:
The code for my button is:
from tkinter import Tk,Button
root=Tk()
Button(root,text='OK').pack()
Another alternative to create button is to create a label and bind it to the action functions. In the below example .bind() is used to connect the label with respective function. You can design according to your requirements.
from tkinter import *
def OnPressed(event):
print('Hello')
def OnHover(event):
But.config(bg='red', fg='white')
def OnLeave(event):
But.config(bg='white', fg='black')
root = Tk()
But = Label(root, text='Hi', bg='white', relief='groove')
But.place(x=10, y=10, width=100)
But.bind('<Button-1>', OnPressed)
But.bind('<Enter>', OnHover)
But.bind('<Leave>', OnLeave)
root.mainloop()
The themed widgets are in 'themed Tk' aka ttk.
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
ok = ttk.Button(root, text='OK')
ok.pack()
root.mainloop()
Avoid from tkinter import * as both tkinter and tkinter.ttk define Button and many other widgets.
If you use this on Windows you should get something looking like a native button. But this is a theme and can be changed. On Linux or MacOS you will get a button style that is appropriate to that platform.
vol_up = Button(root, text="+",activebackground='orange',bg='yellow')
vol_up.pack(side='top')

Close button tkinter after pressing button?

I created a button that works perfectly (not entire code here) but I want that once you press the 'Save' button, the window disappear. Someone knows how to do it?
root2 = tk.Tk()
root2.geometry('200x100')
save_button = tk.Button(root2)
save_button.configure(text='Save', command=lambda: ask_parameter(ents1))
save_button.pack()
root2.mainloop()
Based on the extremely limited snippet of code in your question: I would suggest it doing by defining a function to call that does something like this:
import tkinter as tk
def ask_and_close(root, ents):
ask_parameter(ents)
root.destroy()
ents1 = "something"
root2 = tk.Tk()
root2.geometry('200x100')
save_button = tk.Button(root2)
save_button.configure(text='Save', command=lambda: ask_and_close(root2, ents1))
save_button.pack()
root2.mainloop()
Note: If you're creating multiple windows, I wouild suggest using tk.Toplevel() instead of calling tk.TK() more than once.
Just use the master.quit() method!
Example Code:
from tkinter import *
class Test:
def __init__(self):
self.master = Tk()
self.button = Button(self.master, text="Push me!", command=self.closeScreen)
self.button.pack()
def closeScreen(self):
# In your case, you need "root2.quit"
self.master.quit()
test = Test()
mainloop()
I would suggest using destroy() method as used here https://docs.python.org/3.8/library/tkinter.html#a-simple-hello-world-program.
One of the easy ways to invoke the destroy method in your code is this;
def ask_parameter_and_destroy(ents1):
ask_parameter(ents1)
root2.destroy()
root2 = tk.Tk()
root2.geometry('200x100')
save_button = tk.Button(root2)
save_button.configure(text='Save', command=lambda: ask_parameter_and_destroy(ents1))
save_button.pack()
root2.mainloop()
You can read about differences between destroy() and previously proposed quit() methods on the following page: What is the difference between root.destroy() and root.quit()?.
If your goal is to create a dialog for saving a file, you might be interested in tkinter.filedialog library, which has ready made dialog boxes for handling file saving.

Simple Button Hide on Click

Hello I'm obviously not too experienced with tkinter very much and I couldnt find anything on what I was looking for, maybe someone could help me
def hide(x):
x.pack_forget()
d=Button(root, text="Click to hide me!" command=hide(d))
d.pack()
I would like it so that on click the command runs but the button is not defined when calling the command
You can not use anything if you are still building. Must you use configure and lambda function:
from tkinter import *
def hide(x):
x.pack_forget()
root = Tk()
d=Button(root, text="Click to hide me!")
d.configure(command=lambda: hide(d))
d.pack()
root.mainloop()
First, define the button, then add the command with the config method.
from tkinter import *
root = Tk()
def hide(x):
x.pack_forget()
d=Button(root, text="Click to hide me!")
d.pack()
d.config(command=lambda: hide(d))
root.mainloop()

Categories