Disable making another TopLevel if it already exists - python-3.x

I have an Instructions Window asTopLevel in my app. For now it looks like:
def instructions(self):
window = Toplevel(takefocus = True)
window.geometry("200x200")
window.resizable(0, 0)
Label(window, text = "WIP").grid()
So it's part of the main class and I define a command to call when the user presses a button in the top menu or presses F3 a shortcut I defined.
What I need is when that window is once there I want it to take focus rather that opening a new one.
It could look like:
if window == exists:
window.takefocus
else:
do the upper and create it ....
also upon destruction in need to know that it hasbeen destoyed, otherwise I'll be able to open it only once

This seems to work:
def instructions(self):
if self.window == None:
self.window = Toplevel(takefocus = True)
self.window.focus()
self.window.geometry("200x200")
self.window.resizable(0, 0)
Label(self.window, text = "WIP").grid()
self.window.protocol("WM_DELETE_WINDOW", self.windowclosed)
else:
self.window.focus()
def windowclosed(self):
self.window.destroy()
self.window = None

Related

Disable mouse double click event in tkinter

I am thinking about disabling the mouse double click event after one event. If i double click on an item from the list box the event disables until i press the Enable Double click button. How to archive this?
from tkinter import *
def go(event):
cs = Lb.curselection()
# Updating label text to selected option
w.config(text=Lb.get(cs))
# Setting Background Colour
for list in cs:
if list == 0:
top.configure(background='red')
elif list == 1:
top.configure(background='green')
elif list == 2:
top.configure(background='yellow')
elif list == 3:
top.configure(background='white')
top = Tk()
top.geometry('250x275')
top.title('Double Click')
# Creating Listbox
Lb = Listbox(top, height=6)
# Inserting items in Listbox
Lb.insert(0, 'Red')
Lb.insert(1, 'Green')
Lb.insert(2, 'Yellow')
Lb.insert(3, 'White')
# Binding double click with left mouse
# button with go function
Lb.bind('<Double-1>', go)
Lb.pack()
# Creating Edit box to show selected option
w = Label(top, text='Default')
w.pack()
# Creating Enable button to enable double clicking
enable_btn = Button(top, text = 'Enable Double Click')
enable_btn.pack(pady = 10)
top.mainloop()
To disable double-click, bind to that event and then have your function return the string "break". You can use a variable to trigger that behavior.
Start by defining a flag, and then a function that can set the flag:
double_click_enabled = False
def enable_double_clicking(enable):
global double_click_enabled
double_click_enabled = enabled
Next, define your button to call this function. In this example I'll show two buttons for enabling and disabling:
enable_btn = Button(top, text = 'Enable Double Click', command=lambda: enable_double_clicking(True))
disable_btn = Button(top, text = 'Disable Double Click', command=lambda: enable_double_clicking(False))
Finally, check for the flag at the top of your go function. If double-clicking is disabled, return the string "break" which prevents the result of the function from executing and also disables any default behavior.
def go(event):
if not double_click_enabled:
return "break"
...

tkinter, print text when hovering over OptionMenu objects

So I got to change the colors as well as change colors while hovering over menu objects created using the OptionMenu in tkinter.
I even have text printing when I hover over the button, but the second I drop down the menu, it won't print anymore.
What am I doing wrong? How can I print when you click to open the OptionMenu and and move around through the selections?
from tkinter import *
import tkinter as tk
OZoneIzotopeSemiWhite = "#c0c4ca"
buttonBackground = "#303336"
buttonforeground = "#cdd0d7"
BACKGROUND2 = "#1e1f21"
class DropDownButton():
def __init__(self, parent, placement, opTions, **kw):
self.parent = parent
self.options = opTions
self.om_variable = tk.StringVar(self.parent)
self.om_variable.set(self.options[0])
self.om_variable.trace('w', self.option_select)
self.om = tk.OptionMenu(self.parent, self.om_variable, *self.options)
self.om["menu"].config(fg=buttonforeground, bg=buttonBackground, activebackground=OZoneIzotopeSemiWhite, activeforeground=BACKGROUND2, borderwidth = 0)
self.om.config(fg=buttonforeground, bg=buttonBackground, activebackground=OZoneIzotopeSemiWhite, activeforeground=BACKGROUND2, bd =0)
self.om.place(x = placement, y = 2)
self.om.bind("<Enter>", self.on_enter)
self.om.bind("<Leave>", self.on_leave)
def on_enter(self, event):
if self.om == self.options[0]:
print ("Hello")
elif self.om_variable.get() == self.options[1]:
print ("Hello 2!")
else:
print("Hell0 3!")
def on_leave(self, enter):
print ("leave")
def option_select(self, *args):
print (self.om_variable.get())
root = tk.Tk()
DropDownButton(root, 55, ['one', 'two', 'three'])
root.mainloop()
You're not doing anything wrong as such, but you might need to alter what you do or your expectations. When a menu is popped up by clicking on a menubutton widget — as created by the option menu code — the mouse pointer is grabbed by the menu that has popped up, and this state continues until you do an action that selects something (or you click or release outside the menu, which cancels). Your styling, which depends on the state of the menubutton, might be noticing this and going into something slightly unexpected to you.
By the way, if you pop up the menu using key bindings, the differing look is actually useful as the focus will also be on the actual popup menu.

Problems with Focus

I'm trying to return the focus to the first entry. If you move the focus to the next entry or the button and the you click on the button, the focus returns fine to first entry. When I try doing the same thing by using the tab key, the focus_set method fails. I've tried many different ways, but the result is always the same. Anyone knows why? And might be so kind as to showing me how to do it right? Thanks in advance.
This is what I got so far:
from tkinter import *
w = Tk()
def focus():
box1.focus_set()
def check(event):
if str(event.widget) == '.!entry2':
print('focus back to box1')
focus()
box1 = Entry(w, width=15)
box2 = Entry(w, width=15)
box1.focus_set()
box2.bind('<Tab>', check)
box1.pack()
box2.pack()
btn = Button(w, text='Box 1 Focus', command=focus)
btn.pack()
w.mainloop()
If I run your code, str(event.widget) is something like ".36580648", not ".!entry2". You can give your widget a custom name like
box2 = Entry(w, width=15, name='second')
You can then check if str(event.widget) == '.second'.
Alternatively, you can just check if event.widget == box2: which is easier and less prone to error.
If you do one of these things, you will see that 'focus back to box1' is printed, but the focus is still transferred to the button instead of the label. This is because your custom event is triggered before the default event for <Tab>, which is to move focus to the next widget. You can stop the default event handling by returning 'break' in your function.
The complete example would become:
from tkinter import *
w = Tk()
def focus():
box1.focus_set()
def check(event):
if event.widget == box2:
print('focus back to box1')
focus()
return 'break'
box1 = Entry(w, width=15)
box2 = Entry(w, width=15)
box1.focus_set()
box2.bind('<Tab>', check)
box1.pack()
box2.pack()
btn = Button(w, text='Box 1 Focus', command=focus)
btn.pack()
w.mainloop()

Juxtapose a GtkMenuBar with other widgets?

Is it possible to place a GtkMenuBar with other widgets together , instead of showing at the top of that window ?
Or i could use buttons , but when you hover your mouse over those buttons , the menu on the buttons won't just behave like a menu , which is , when you are close to one menu item , the menu pops down directly , without click , and other menu hide automatically. Can i make buttons like that ? Or other widgets that could have: label , image , and pop down menu item is cool.
Any ideas is appreciated.
Maybe the "enter-notify-event" and "leave-notify-event", connected to buttons, may help you do the thing, with for example, a popup menu show and hide respectively.
EDIT
I finally forgot those "enter" and "leave" events whose behaviour was a little complex, and just used the "motion-notify-event"...
Now I hope it is what you want !
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class MenuExample:
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_size_request(200, 100)
self.window.set_title("GTK Menu Test")
self.window.connect("delete_event", lambda w,e: gtk.main_quit())
# A vbox to put a button in:
vbox = gtk.VBox(False, 0)
self.window.add(vbox)
vbox.show()
self.popped = False
# Create a button to simulate a menu
button = gtk.Button("press me")
vbox.pack_start(button, False, False, 2)
self.window.add_events(gtk.gdk.POINTER_MOTION_MASK)
self.window.connect("motion-notify-event", self.wakeup)
self.window.show_all()
self.bmenu = gtk.Button("A single entry menu")
self.bmenu.connect("clicked", self. menuitem_response, "Click on the magic menu !")
vbox.pack_start(self.bmenu, False, False, 2)
def wakeup(self, widget, event):
#print "Event number %d woke me up" % event.type
(x, y) = self.window.get_pointer()
if y < 30:
if self.popped == False:
self.popped = True
self.bmenu.show()
elif y > 60:
if self.popped == True:
self.popped = False
self.bmenu.hide()
# Print a string when a menu item is selected
def menuitem_response(self, widget, string):
print "%s" % string
def main():
gtk.main()
return 0
if __name__ == "__main__":
MenuExample()
main()

wxpython run when textctrl changes

I am making a simple text editor in wxpython. I would like it to be able to edit code such as python, and as such I would like to have it highlight the text in a similar manner to IDLE or Notepad++. I know how I would highlight it, but I would like the best way of running it. I don't know if it is possible but what I would really like is to run whenever a key is pressed, and not on a loop checking if it is pressed, so as to save on processing.
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(500,600))
style = wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_RICH2
self.status_area = wx.TextCtrl(self, -1,
pos=(10, 270),style=style,
size=(380,150))
self.status_area.AppendText("Type in your wonderfull code here.")
fg = wx.Colour(200,80,100)
at = wx.TextAttr(fg)
self.status_area.SetStyle(3, 5, at)
self.CreateStatusBar() # A Statusbar in the bottom of the window
# Setting up the menu.
filemenu= wx.Menu()
filemenu.Append(wx.ID_ABOUT, "&About","Use to edit python code")
filemenu.AppendSeparator()
filemenu.Append(wx.ID_EXIT,"&Exit"," Terminate the program")
# Creating the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
self.Show(True)
app = wx.App(False)
frame = MainWindow(None, "Python Coder")
app.MainLoop()
If a loop is needed what would be the best way to make it loop, with a while loop, or a
def Loop():
<code>
Loop()
My new code with the added bind:
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(500,600))
style = wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_RICH2
self.status_area = wx.TextCtrl(self, -1,
pos=(10, 270),style=style,
size=(380,150))
#settup the syntax highlighting to run on a key press
self.Bind(wx.EVT_CHAR, self.onKeyPress, self.status_area)
self.status_area.AppendText("Type in your wonderfull code here.")
fg = wx.Colour(200,80,100)
at = wx.TextAttr(fg)
self.status_area.SetStyle(3, 5, at)
self.CreateStatusBar() # A Statusbar in the bottom of the window
# Setting up the menu.
filemenu= wx.Menu()
filemenu.Append(wx.ID_ABOUT, "&About","Use to edit python code")
filemenu.AppendSeparator()
filemenu.Append(wx.ID_EXIT,"&Exit"," Terminate the program")
# Creating the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
self.Show(True)
def onKeyPress (self, event):
print "KEY PRESSED"
kc = event.GetKeyCode()
if kc == WXK_SPACE or kc == WXK_RETURN:
Line = self.status_area.GetValue()
print Line
app = wx.App(False)
frame = MainWindow(None, "Python Coder")
app.MainLoop()
In your MainWindow __init__ function add this
self.Bind(wx.EVT_CHAR, self.onKeyPress, self.status_area)
then define onKeyPress in MainWindow
def onKeyPress (self, event):
kc = event.GetKeyCode()
if kc == WXK_SPACE or kc == WXK_RETURN:
#Run your highlighting code here
Come to think of it, this might not be the most efficient way of doing code highlighting. Let me look this up. But in the meantime you can try this.
Edit:
Take a look at this - StyledTextCtrl . I think its more along the lines of what you need.
I solved this when I faced the same issue by creating a custom event.
First, I created a subclass of the TextCtrl, so I had a place in code to raise/post the custom event from:
import wx.lib.newevent
(OnChangeEvent, EVT_VALUE_CHANGED) = wx.lib.newevent.NewEvent()
class TextBox(wx.TextCtrl):
old_value = u''
def __init__(self,*args,**kwargs):
wx.TextCtrl.__init__(self,*args,**kwargs)
self.Bind(wx.EVT_SET_FOCUS, self.gotFocus) # used to set old value
self.Bind(wx.EVT_KILL_FOCUS, self.lostFocus) # used to get new value
def gotFocus(self, evt):
evt.Skip()
self.old_value = self.GetValue()
def lostFocus(self, evt):
evt.Skip()
if self.GetValue() != self.old_value:
evt = OnChangeEvent(oldValue=self.old_value, newValue=self.GetValue())
wx.PostEvent(self, evt)
Now, in my frame's code, here is a snippet of me Binding the event, and using it.
summ_text_ctrl = TextBox(self, -1, size=(400, -1))
summ_text_ctrl.Bind(EVT_VALUE_CHANGED, self.onTextChanged)
def OnTextChanged(self, evt):
evt.Skip()
print('old Value: %s' % evt.oldValue )
print('new Value: %s' % evt.newValue )

Resources