string variable not working with - python

I have my main window, which i refer to as mainroot, and when i press a button the secondary window root should appear
in root there is an entry and i want it to be assigned to "hohoho" whenever i press anykey, so i tied it to KeyRelease event.
All i mentioned above is working when there is no mainroot, As in this code:
import tkinter as Tk
# mainroot=Tk.Tk()
root=Tk.Tk()
root.geometry("200x200")
myvar=Tk.StringVar()
entry=Tk.Entry(root,)
entry.pack()
entry.configure(textvariable=myvar)
entry.bind('<KeyRelease>',lambda event:printing())
def printing():
myvar.set("hohohohohohoho")
print('myvar is',myvar.get())
print('value is',entry.get())
root.mainloop()
# mainroot.mainloop()
but if you commented the root.mainloop() and uncommented root.mainloop() and # mainroot.mainloop() the text shown in entrystops following myvar.
what i found is that myvar is not working properly as it should.
anyone have any idea what am doing wrong or is this bug in tkinter or what?????
I know that entry.trace() is more elegant but i didn't use it because i am not very familiar with it.
thank you
any hint will be appreciated

Related

How to deselect an entry in Tkinter

A friend of mine was making a basic program using tkinter when he realized that even when you deselect an entry, the cursor still remains in that entry until you select another one. My question is, is this a technical issue? Also, is there any way for me to fix this?
use widget.focus_set() to change focus when mouse button is pressed
from tkinter import *
def change_focus(event):
event.widget.focus_set()
root = Tk()
Entry(root).pack()
Entry(root).pack()
root.bind_all('<Button>', change_focus)
root.mainloop()

Can I call a function by clicking 'OK' in messagebox.showinfo?

I have added a message box in my code and I want to call a function defined earlier when clicking the 'OK' button in the message box. Is there any way to achieve this? Thanks in advance.
messagebox.showinfo('Correct', 'Correct!\nClick OK to continue')
When you make a showinfo box, it actually halts your script until you press OK. All you will need to do is put the function you want right after it.
See this example:
import tkinter as tk
from tkinter import messagebox
def go():
messagebox.showinfo('Correct', 'Correct!\nClick OK to continue')
print('yeah')
root = tk.Tk()
btn = tk.Button(root, text='click', command=go)
btn.pack()
root.mainloop()
If you run that, you will see the print('yeah') only happens when the user clicks OK. So, you can do something similar and call the function where I have put the print.
I had a similar problem and looked for a simple code. You can use:
from tkinter import Tk,messagebox
root=Tk()
result=messagebox.showinfo('Correct','Correct!')
root.destroy()
go()
I used root.destroy() because sometimes I had problems closing the popup window.
In tkinter.messagebox.showinfo your only option is to press OK or close the tkinter popup window, both has the same effect and returns a string "ok".
If you instead want to call a function only when you press yes use tkinter.messagebox.askyesno:
from tkinter import Tk,messagebox
root=Tk()
result=messagebox.askyesno('Correct','Correct!')
root.destroy()
if result==True:
go()

Tkinter - can't type into entry widget after restoring the window with wm_state('normal') function

I can't find any solution, so I hope you will help me.
I'm using in my project entry widgets and wm_state('iconic') and wm_state('normal) function. Unfortunatelly after restoring tkinter main window by using wm_state('normal) function I'm unable to use entry widget. Manually minimizing and restoring the window solves the problem. Do you have any idea, how can I avoid it? Below is the testing code I prepared for better understanding. Thank you for the help. I thought root.withdraw() is the solution, but unfortunately, program disappears then from the taskbar, what is not good in my case.
import tkinter as tk
from tkinter import ttk
import time
root = tk.Tk()
root.geometry('200x200')
def test_function():
root.wm_state('iconic')
time.sleep(0.5)
root.wm_state('normal')
button = tk.Button(root, text='MINIMIZE BUTTON', command=test_function)
button.grid(row=0, column=0)
entrywidget = tk.Entry(root, width='10')
entrywidget.grid(row=1, column=0)
entrywidget.focus()
root.mainloop()
Using:
root.withdraw()
root.deiconify()
Instead of:
root.wm_state('iconic')
root.wm_state('normal')
Solves the entry widget problem, but program is no longer visible on the taskbar after root.withdraw() until root.deiconify() method call.

Get position in tkinter Text widget

I'm trying to find a reliable way of getting the current cursor position in a tkinter text widget.
What I have so far is:
import tkinter as tk
def check_pos(event):
print(t.index(tk.INSERT))
root = tk.Tk()
t = tk.Text(root)
t.pack()
t.bind("<Key>", check_pos)
t.bind("<Button-1>", check_pos)
root.mainloop()
However, this prints the previous cursor position and not the current one. Anyone have any ideas what is going on?
Thanks in advance.
Thanks to Bryan Oakley for pointing me in the right direction with the links he posted in the comments. I chose the third choice which introduces an additional binding. The working code is below. Now the binding happens after the class is bound so that the change of position in the Text widget is visible to the function.
import tkinter as tk
def check_pos(event):
print(t.index(tk.INSERT))
root = tk.Tk()
t = tk.Text(root)
t.pack()
t.bindtags(('Text','post-class-bindings', '.', 'all'))
t.bind_class("post-class-bindings", "<KeyPress>", check_pos)
t.bind_class("post-class-bindings", "<Button-1>", check_pos)
root.mainloop()

How to show a window that was hidden using "withdraw" method?

I would like to show a window after I called withdraw.
The following is my current code:
from Tkinter import *
def callback():
global root
root.withdraw()
win2 = Tk()
root = Tk()
Label(root,text='this is a window').pack()
Button(root,text='withdraw',command=self.callback).pack()
mainloop()
As soon as I press the button, the window disappears much as I want it, and another window appears and everything works great. How do I get the first window back, in the same state as it was before?
Use the following commands when you want to show the window:
# root.update() # not required
root.deiconify()
If you want to know more about it, see here.

Categories