Tkinter bind executes immediately when script is run - python-2.7

I've laid out a frame, part of which is:
ticketID = IntVar()
ticketID.set(ticket)
ticketfield = Label(titleframe, text = 'Ticket : ')
ticketfield.grid(row=0, column=0)
ticketfieldentry = Entry(titleframe, textvariable=ticketID)
ticketfieldentry.grid(row=0, column=1)
ticketfieldentry.bind("<Double-Button-1>", searchforticket(ticketfieldentry.get()))
And a placeholder:
def searchforticket(ticket):
searchforstring = "This would search for ticket %s" % ticket
tkMessageBox.showinfo('Search by ticket', message = searchforstring)
Ticket is passed to the script at run time, or is assumed None. What I thought the above would do is create an Entry box that would display the ticket number, or could have one entered directly. After being entered, double clicking would bring up the searchforticket function.
However, when I run the script, searchforticket is run immediately, with whatever is being assigned to ticket and then after I click past the tkMessageBox is when the window containing the ticketfieldentry and everything else renders.
I am not seeing at all what could be causing this, but I assume it is something dead obvious to someone else.

searchforticket(ticketfieldentry.get() cause immediate call. Change as follow:
ticketfieldentry.bind("<Double-Button-1>", lambda e: searchforticket(ticketfieldentry.get()))

Related

[Python2.7 TKinter]How to gray out a option in optionmenu that has been selected by older instances?

Here's my code:
class DefaultServiceClassWidget(ServiceClassWidget):
def __init__(self, mpet_widget, sc_data, idx):
super(DefaultServiceClassWidget, self).__init__(mpet_widget, sc_data, idx, DefaultServiceClassRow.number)
delete_default_sc_button = Button(mpet_widget.listFrame,justify=LEFT,text="x",fg="red",command= lambda: mpet_widget.delete_sc(self.idx))
delete_default_sc_button.grid(column=4,row=DefaultServiceClassRow.number)
self.select_default_class_label = Label(mpet_widget.listFrame,anchor=W,justify=LEFT,text="Select a Class")
self.select_default_class_label.grid(column=0,row=DefaultServiceClassRow.number)
options = ["All","CS Registration","GPRS Attach","PDP Activation","SMS","Reset","USSD","LTE"]
self.menu_pick_a_class = OptionMenu(mpet_widget.listFrame, sc_data.get_name(), *options, command=lambda event: sc_data.set_id())
self.menu_pick_a_class.grid(column=1,row=DefaultServiceClassRow.number)
self.row = DefaultServiceClassRow.number
DefaultServiceClassRow.number = DefaultServiceClassRow.number+2
def delete(self):
DefaultServiceClassRow.number = DefaultServiceClassRow.number - 2
default_list = (list(self.mpet_widget.listFrame.grid_slaves(row=self.row)) +
list(self.mpet_widget.listFrame.grid_slaves(row=self.row+1)))
for l in default_list:
l.grid_remove()
What happens is that there's a button connected to this function, every time the button is clicked, this function gets called and created a new grid on the GUI. What I want is that if for example "SMS" in optionmenu is selected, the next time this function gets called, "SMS" option will be grayed out (i.e. each option can be only selected once in the program)
I've tried updating status of the selected option by using status = "disabled" but it only works for the same grid, once a new grid(instance) is created, everything gets reset and all the options became available again :(
Also, in the same grid, if I disabled an option by selecting it and then changed to something else, the original selection is still grayed and cannot be selected again - I know why this happens but how do I fix it?
Sorry for the long question but I can't seem to find a solution online :(
Thank you in advance!

messagebox getting looped

I am displaying a message box on click of option menu when data is not available and is to be acquired. But somehow whenever I change content of the option menu the message box gets looped. Like- first time it vanishes when I press ok. when I change option again second time it displays message box two times and third time three times. Can someone tell me what to change in my code?
#the function where I change option menu value
def module_func(event):
#declarations
if(dateText.get()!="" and driveText.get()!=""):
global module_dir
global selected_module
modules['menu'].delete(0,END)
select="-----Select-----"
modules['menu'].add_command(label=select, command=tk._setit(module,select))
for module_dir in get_immediate_subdirectories(('%s\%s\%s')%(startPath,selected_shot,selected_fec)):
module_dropdown.append(module_dir)
modules['menu'].add_command(label=module_dir, command=tk._setit(module,module_dir))
module.trace('w',setEightText) #think problem is here
else:
errormsg("")
def setEightText(*args):
#declarations
if(os.path.exists(('%s\%s\%s\%s\%s')%(startPath,selected_shot,selected_fec,selected_module,chan))):
#some operations
else:
tkMessageBox.showinfo("Error","Data at given location not available.\n Please Wait until data is acquired.")
module=tk.StringVar(labelframe1)
module.set("-----Select-----")
modules = tk.OptionMenu(labelframe1, module, module_dropdown, command=module_func)
modules.config(width=20, background="SNOW")
modules.grid(row=8, column=2, sticky="W", padx=10, pady=5)
modules.bind('<Button-1>',module_func)
I think the problem is because of trace. But don't know what to add instead of that.

Tabbed GUI keeps hanging on entry box

I have a very primitive GUI built with Tkinter. This is my first attempt at a GUI so I am struggling to understand what is going on (and with syntax). Here is what I have:
from __future__ import division, print_function
import os, ttk, tkFileDialog, Tkconstants
import Tkinter as tk
import datetime as dt
Tkinter = tk
# define OS version
version = '0.0.2'
class TestGUI(tk.Tk):
def __init__(self,parent):
tk.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
# try building list of instruments and sites
if os.path.isfile('config'):
with open(''config','r') as config:
config = dict( [(r.split('=')[0].strip(), r.split('=')[1].strip()) for r in config.read().split('\n') if r[0]<>'#'] )
self.datapath = config['datapath']
else:
self.datapath = '../'
def initialize(self):
self.grid()
# set up tabs
self.geometry( "%dx%d+%d+%d" % (1500, 900, 200, 50) )
nb = ttk.Notebook(self)
nb.pack(fill='both',expand='yes')
f1 = tk.Frame(bg='green')
f2 = tk.Frame(bg='blue')
f3 = tk.Frame(bg='red')
f1.grid()
f2.grid()
f3.grid()
nb.add(f1, text='General'.ljust(12,' '))
nb.add(f2, text='Plot'.ljust(12,' '))
nb.add(f3, text='Analysis'.ljust(12,' '))
button = tk.Button(f2,text='I AM A BUTTON!')
button.pack(side='left', anchor='nw', padx=3, pady=5)
# insert button and text box for specifying data location
path_button = tk.Button(f1,text='Browse',command=self.askdirectory).pack(side='left', anchor='nw', padx=10, pady=15)
self.path_entry = tk.StringVar()
self.entry = tk.Entry(f1,textvariable=self.path_entry)
self.entry.grid(column=12,row=8,columnspan=10)
self.entry.bind("<Return>", self.OnPressEnter)
self.path_entry.set(u"Sites directory path...")
def OnButtonClick(self):
print("You clicked the button !")
def OnPressEnter(self,event):
print("You pressed enter !")
def askdirectory(self):
"""Returns a selected directoryname."""
self.datapath = tkFileDialog.askdirectory()
self.path_entry.set(self.datapath)
if __name__ == "__main__":
app = TestGUI(None)
app.title(version)
app.mainloop()
My problem is centered around the addition of an entry box here:
self.path_entry = tk.StringVar()
self.entry = tk.Entry(f1,textvariable=self.path_entry)
self.entry.grid(column=12,row=8,columnspan=10)
self.entry.bind("<Return>", self.OnPressEnter)
self.path_entry.set(u"Sites directory path...")
If I run the code as-is, it just hangs (it also hangs if I use "f2"; I suspect it is getting caught in the infinite loop without actually doing anything). However, if I change the parent from "f1" to "f3" or it works (the entry box is now in frame 3 instead of frame 1, but it at least does not hang on me). There is another issue even when f3 is used: the entry box's width/position never change despite my changing of column/row/columnspan values.
Does anyone know why the code is hanging when I specify "f1" or "f2" and how to fix it?
Does anyone know why my entry box position/size is not changing?
You have put widgets in f1 and f2 using the pack geometry manager:
button = tk.Button(f2,text='I AM A BUTTON!')
button.pack(side='left', anchor='nw', padx=3, pady=5)
#and
path_button = tk.Button(f1,text='Browse',command=self.askdirectory).pack(side='left', anchor='nw', padx=10, pady=15)
Mixing geometry managers can lead to your program hanging, so using grid to put in the Entry does exactly that.
From effbot.org:
Warning: Never mix grid and pack in the same master window. Tkinter will happily spend the rest of your lifetime trying to negotiate a solution that both managers are happy with. Instead of waiting, kill the application, and take another look at your code. A common mistake is to use the wrong parent for some of the widgets.
The problem you describe of the Entry not changing position is because it is the only widget there, so the row(s) and column(s) in which the entry is are the only ones which do not have a width and height of 0. To make rows and columns without widgets take up space, use grid_rowconfigure(index, weight=x) where x is non-zero. An example is given in this answer.
Again from effbot.org:
weight=
A relative weight used to distribute additional space between rows. A row with the weight 2 will grow twice as fast as a row with weight 1. The default is 0, which means that the row will not grow at all.

Sikuli api - "OK" click ignored during file upload

Using:
Sikuli-api stand alone jar
python 2.7.6 with pyjnius
Ubuntu 14.04 Trusty
Issue:
The Sikuli DesktopMouse click() method works in almost all situations except when I'm trying to click "Open" on a file upload [Ubuntu Open](http://i.stack.imgur.com/nZsvv.png) from the upload dialog or "Allow" on the Flash Player Settings dialog [Flash Player Settings](http://i.stack.imgur.com/7OrbG.png). If I navigate to these areas manually I can click on these buttons without error. I have to imagine that it is some sort of permissions issue (because all other clicks work including all the clicks in the code snippet below. I mean, the exception at the bottom is thrown, meaning that the "Open" button is both seen and clicked on, twice), but I haven't the foggiest what that would entail. There are no errors to share because the click doesn't technically fail
About Code:
self.get_target() returns an ImageTarget
self.mouse and self.kb are instances of DesktopMouse and DesktopKeyboard respectively.
self.box creates a DesktopCanvas and draws a box around the target DesktopScreenRegion
Code:
def upload_file(self, path, title):
screen = sikuli.DesktopScreenRegion()
locationTarget = self.get_target("images/ubuntu_location_field.png")
locationTarget.setMinScore(.6)
locationField = screen.wait(locationTarget, 5000)
if locationField is None:
editTarget = self.get_target("images/ubuntu_location_edit.png")
edit = screen.wait(editTarget, 5000)
if edit is None:
searchTarget = self.get_target("images/ubuntu_upload_search.png")
search = screen.wait(searchTarget, 5000)
self.box(search)
self.mouse.click(search.getCenter())
else:
self.box(edit)
self.mouse.click(edit.getCenter())
locationField = screen.wait(locationTarget, 5000)
self.mouse.click(locationField.getCenter())
self.kb.type(path + "/" + title)
okTarget = self.get_target("images/ubuntu_upload_opensave.png")
ok = screen.find(okTarget)
self.mouse.click(ok.getCenter())
if screen.find(okTarget):
self.mouse.click(ok.getCenter())
if screen.find(okTarget):
#GETTING TO THIS POINT MEANS THAT THE BUTTON CAN BE SEEN AND CLICKED
#BUT THAT THE CLICK IS NOT DOING ANYTHING
raise Exception("Upload file is acting like a bitch.")
sikuli.API.pause(10000)
Found that the issue stemmed from using the firefox webdriver, which apparently has much higher security than its chrome counterpart. All operations can be done on chromedriver.

How do I wait for a certain number of buttons to be clicked in Tkinter/Python?

I'm trying to write a simple 'Simon' game, but I have hit a road block here, and honestly have no idea how to get around it.
So here, I made a class for the four buttons in the GUI:
class button:
def buttonclicked(self):
self.butclicked= True
def checkIfClicked(self):
if self.butclicked== True:
global pressed
pressed.append(self.color)
self.butclicked= False
def __init__(self, color1):
self.color= color1
self.button= tk.Button(root, text=' '*10, bg= self.color, command= self.buttonclicked)
self.button.pack(side='left')
self.butclicked=False
I then created four instances of this class in blue, red, yellow, and green as bb, rb, yb, and gb.
Once everything is packed into the Tk() module, it enters a while loop that appends a random color to a list activecolors. I try to use the following loop to wait until the list pressed is at least as long as the list activecolors before comparing the two to see if the user was correct:
while len(pressed)<len(activecolors):
sleep(.25)
print('In the check loop')
bb.checkIfClicked()
rb.checkIfClicked()
yb.checkIfClicked()
gb.checkIfClicked()
However, since it is stuck inside the while loop, the program can't tell that the button has been clicked. I thought adding the sleep method into the loop would allow the code to have time to do other things (such as process button clicks), but this is not the case. Any help is appreciated.
Here is the link to the full code, if you would like to see it. A warning though: it's not pretty.
Edit:
I ended up just changing the code to check the list only after a new button was clicked, telling the computer the code was ready. I've updated the Google Document if you'd like to see it.
You are making it too complicated. This program uses partial from functiontools to allow a variable to be passed to the function so one function handles all clicks (Python 2.7).
from Tkinter import *
from functools import partial
class ButtonsTest:
def __init__(self):
self.top = Tk()
self.top.title('Buttons Test')
self.top_frame = Frame(self.top, width =400, height=400)
self.colors = ("red", "green", "blue", "yellow")
self.colors_selected = []
self.num_clicks = 0
self.wait_for_number = 5
self.buttons()
self.top_frame.grid(row=0, column=1)
Button(self.top_frame, text='Exit',
command=self.top.quit).grid(row=2,column=1, columnspan=5)
self.top.mainloop()
##-------------------------------------------------------------------
def buttons(self):
for but_num in range(4):
b = Button(self.top_frame, text = str(but_num+1),
command=partial(self.cb_handler, but_num))
b.grid(row=1, column=but_num+1)
##----------------------------------------------------------------
def cb_handler( self, cb_number ):
print "\ncb_handler", cb_number
self.num_clicks += 1
this_color = self.colors[cb_number]
if (self.num_clicks > self.wait_for_number) \
and (this_color in self.colors_selected):
print "%s already selected" % (this_color)
self.colors_selected.append(this_color)
##===================================================================
BT=ButtonsTest()