Cannot access text entry box after reopen the GTK window - python

First of all, this issue is only happened in WIN7, and it is normally under raspberry pi (Debian Linux).
I have two window, the main window and a child window. The main window has a button which can activate the child window. The child window has a text entry box which can input the strings. The issue is when activate the child window at 1st time, the text entry box is functional. But when the child window is closed and re-opened, the text entry box seems disabled that cannot input any text into it, even set_text("xx") function cannot write any text into it.
The detailed steps are:
run the py script
click the button on main window to open the child window. I have tried with the below three methods, it seems they have same issue:
def on_button_clicked(self, widget, data=None):
self.child_window.present()
#self.child_window.show()
#self.child_window.show_all()
Now the child window is opened and the text entry box is functional, I can type any text into it.
Close the child window. I have bind the delete signal on to the child window. So every time the child window is closed, the below function will be executed, which will hide the current child window.
def on_WindowOfScanning_delete_event(self, widget, data=None):
self.child_window.hide()
return True
Now the main window is on focus and click the button to activate the child window again.
self.child_window.present()
Now the child window is appear, but the text entry box seems disabled.
Any one can help me on this issue? Appreciated for that..
The version information is: Python 2.7.3 GTK 2.24.2, and I use glade to manage the GUI interface.
================= The same question with a different example: =====================
http://www.pygtk.org/pygtk2tutorial/sec-TextEntries.html#entryfig
This link is the pygtk's official example. While running on my WIN7(64bit) system, the text entry box cannot be edited since the first time opening. But if you move mouse to activate other window, and turn back to this gtk window, the text entry box can be edited then. I am not sure if this is a pygtk's bug.
I have tried python 2.6.6 and 2.7.3 with pygtk2.24.2-all-in-one.
================= The solution to this issue: =====================
It seems no one have such kind of problem so I post my own solution.
1st, give up using the window.hide() function.
2nd, destroy the child window every time it finished its job, and re-init the gtk.Window again to invoke the child window. Here is a simple example:
#!/usr/bin/env python
import pygtk
pygtk.require( "2.0" )
import gtk
class PopupExample(gtk.Window):
def __init__( self ):
gtk.Window.__init__(self)
self.connect("destroy", lambda *w: gtk.main_quit())
button = gtk.Button("Popup Window")
button.connect("clicked", self.show_popup_window)
self.add(button)
def show_popup_window(self, button):
popup = gtk.Window()
popup.add(gtk.Entry())
popup.show_all()
if __name__ == "__main__":
pe = PopupExample()
pe.show_all()
gtk.main()

I encountered the same problem in gnucash and inkscape,
I solved this problem by going into Control Panel -> Locales and Languages and set format to English(US)

Related

Start Python script GUI minimized / in Windows Tray | Tkinter

I´ve built a simple GUI app, and I´m playing with pystray.
Actually, my script starts as usual, the first thing you see is the GUI.
If you klick on exit, the GUI minimize, and the tray Icon spawns.
For now, i search a way to start my script in this "tray Mode"
here are some informations:
class Hauptfenster:
# Define a function for quit the window
def quit_window(icon, item):
icon.stop()
fenster.destroy()
# Define a function to show the window again
def show_window(icon, item):
icon.stop()
fenster.after(0, fenster.deiconify())
# Hide the window and show on the system taskbar
#staticmethod
def hide_window():
fenster.withdraw()
image = Image.open(os.path.join(application_path, iconFile))
menu = (item('Beenden', Frontend.Hauptfenster.quit_window), item('Einstellungen', Frontend.Hauptfenster.show_window))
icon = pystray.Icon("name", image, "Quicksafe", menu)
icon.run()
Please ask me if you need some more Information and Thanks a lot !
Best regards
Background:
My programm should lay in the autostart of win10 , but i dont want to minimize the window each time i restart my pc
I just added my method hide_window to my main function,
when script starts, you see something moving on to screen for 2ms.. but it disapears very quick... so thats something I can live with

Unable to lift parent window exist under child window in Tkinter? (Python 3.6.6, Windows 7 Ultimate)

Below is my test module:
import tkinter as tk
def startUi():
window = tk.Tk()
window.title("Parent Window")
window.geometry("500x200")
rgButton = tk.Button(window,text=" Open Child",command=openChildWindow)
rgButton.pack()
window.mainloop()
def openChildWindow():
window = tk.Toplevel()
window.title("Child Window")
window.geometry("200x200")
startUi()
After running the above code why am I not able to lift the parent window to front just after clicking the button. i.e after clicking the button the child window will show in front of parent window, however when I click on the space area of the parent window I am not able to bring the parent window up to the front.
I am able to bring it up to front only by:
Clicking on the border of the parent window
Clicking on child window once and then clicking on the Parent again.
This not a normal behavior. Why is it so? And How will I solve it? I want to achieve the normal behavior i.e. I want to bring the parent window up in front of the child on clicking anywhere on the parent window.

Pyside: Opening a new window with picture

I have created a UI that can be launched inside Maya. My class for this window inherits from QDialog. I want to have a button that will open a jpg into a new window, and then it closes with another button. While this other window is open I would like to still be able to interact with the main window. Is it possible to do this? How would I go about launching this new window?
You can use this code to get what you want. I tested it in Maya 2016.5 on macOS. Works fine!
import maya.cmds as cmds
def loadSecondWindow(*args):
window = cmds.window()
cmds.paneLayout()
cmds.image(image='/Users/swift/Desktop/scientist01.jpg')
cmds.showWindow(window)
def deleteSecondWindow(*args):
if (cmds.window('window2', exists=True)):
cmds.deleteUI('window2')
cmds.window(width=200)
cmds.columnLayout(adjustableColumn=True)
cmds.button(label='Window with Picture', command=loadSecondWindow)
cmds.button(label='Delete Window', command=deleteSecondWindow)
cmds.showWindow()

tk destroy() and grid_forget() don't always work

i'm hoping anyone can help me out here. i'm having an issue with a tkinter gui i built. the issue only happens in windows. My GUI creates a results frame with some labels in it, when it's time to calculate something else, the user clicks on the "newPort" button and that button is supposed to remove the results frame and set to False some instance attributes internal to the calculation. The issue i'm having, which is apparent only in windows is that sometimes the results frame, and its descendant labels don't disappear every time. Sometimes they do, sometimes they don't. The instance variable is correctly set to False but the widgets are still visible on the main GUI. The GUI also contains a couple checkboxes and radiobuttons but they don't impact the creation of the results frame nor its expected destruction. I have not been able to pin point a pattern of actions the user takes before clicking on the newPort button which causes the frame and labels to not get destroyed. This happens when i freeze my app with py2exe, as well as running the app from the python interpreter within the eclipse IDE. I have not tried running the app from the python interpreter directly (i.e. without the IDE) and this problem does not happen on my Mac when i run the app using the eclipse python interpreter. Thanks very much all! My code looks like this:
import Tkinter as TK
class widget(object):
def __init__(self,parent=None):
self.parent = TK.Frame(parent)
self.parent.grid()
self.frame = TK.Frame(self.parent)
self.frame.grid()
newLedger = TK.Button(self.parent,command=self.newPort).grid()
self.calcButton = TK.Button(self.frame,command=self.showResults)
self.calcButton.grid()
self.calcVariable = True
def newPort(self):
self.calcVariable = False
try:
self.second.grid_forget()
self.first.grid_forget()
self.resultsFrame.grid_forget()
self.second.destroy()
self.first.destroy()
self.resultsFrame.destroy()
except:
raise
self.frame.update_idletasks()
def showResults(self):
self.resultsFrame = TK.Frame(self.frame)
self.resultsFrame.grid()
self.first = TK.Label(self.resultsFrame,text='first')
self.first.grid()
self.second = TK.Label(self.resultsFrame,text='second')
self.second.grid()
if __name__ == '__main__':
root = TK.Tk()
obj = widget(root)
root.mainloop()
You don't need to destroy or call grid_forget on the labels, and you don't need to call grid_forget on the resultsFrame; when you destroy the resultsFrame it will cause all off its children to be destroyed, and when these widgets are destroyed they will no longer be managed by grid.
The only way I can get widgets to not be destroyed is if I click on the "calc" button twice in a row without clicking on the "new" button in-between. I'm doing this by running your program from the command line.

wxPython: how to make taskbar icon respond to left-click

Using wxPython, I created a taskbar icon and menu.
Everything works fine (in Windows at least) upon right-click of the icon: i.e., the menu is displayed, and automatically hidden when you click somewhere else, like on Windows' taskbar.
Now I do want to have the menu appear when the icon is left-clicked as well.
So I inserted a Bind() to a left-click in the Frame class wrapper, calling the CreatePopupMenu() of the taskbar icon:
import wx
class BibTaskBarIcon(wx.TaskBarIcon):
def __init__(self, frame):
wx.TaskBarIcon.__init__(self)
self.frame = frame
icon = wx.Icon('test_icon.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(icon, "title")
def CreatePopupMenu(self):
self.menu = wx.Menu()
self.menu.Append(wx.NewId(), "dummy menu ")
self.menu.Append(wx.NewId(), "dummy menu 2")
return self.menu
class TaskBarFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, style=wx.FRAME_NO_TASKBAR)
...
self.tbicon = BibTaskBarIcon(self)
wx.EVT_TASKBAR_LEFT_UP(self.tbicon, self.OnTaskBarLeftClick)
...
def OnTaskBarLeftClick(self, evt):
self.PopupMenu(self.tbicon.CreatePopupMenu())
...
def main(argv=None):
app = wx.App(False)
TaskBarFrame(None, "testing frame")
app.MainLoop()
This works fine, except that the menu does not disappear automatically when you click somewhere else on your screen. In fact, left-clicking multiple times on the icon creates multiple menus. The only way to hide the menu(s) is to click on one of its items (which you don't always want). I've looked at the available methods of TaskbarIcon, but I failed to be clear about which one to use to hide the menu (.Destroy() didn't work). Moreover, I don't know which event to bind it to (there is a EVT_SET_FOCUS, but I couldn't find any EVT_LOOSE_FOCUS or similar).
So, how to hide the menu upon losing focus?
EDIT: I've inserted a bit more code, to make it more clear
Ah, I've discovered what went wrong. In the statement
self.PopupMenu(self.tbicon.CreatePopupMenu())
I had bound the popup menu to the frame, instead of to the taskbar icon.
By changing it to:
self.tbicon.PopupMenu(self.tbicon.CreatePopupMenu())
all is working well now.
Thanks for all remarks
I think the problem here is that the PopupMenu is usually used in a program's context, not a little icon in the system tray. What that means is that in a normal frame, the popup menu would detect the click the you clicked off of it. Here, you are clicking outside of the wxPython program. Also, the PopupMenu is usually used with EVT_CONTEXT_MENU, not this taskbar event.
You can try wx.EVT_KILL_FOCUS and see if that works since it should theoretically fire when you click off the menu. Or you could ask on the official wxPython forum here: http://groups.google.com/group/wxpython-users/topics
Mike Driscoll
Blog: http://blog.pythonlibrary.org

Categories