Discard changes on EditLabel wxtreectrl - python

I have a TreeCtrl where the user is able to edit the label of an item.
However, if the user presses enter when the input is empty, then label is should be set to its former state. I am able to accomplish this, but with a bug.
This is the "workflow"
Label shows 'item'
User starts to edit label
User inputs ''
User presses enter
Label shows ''
User starts to edit label
User changes nothing, inputs ''
User presses enter
Label shows 'item'
Code example
def on_rename(self, event):
treeItemId = self.tree.GetSelection()
self.tree.EditLabel(treeItemId)
def on_rename_end(self, event):
name = event.GetLabel()
treeItemId = self.tree.GetSelection()
pydata = self.tree.GetPyData(treeItemId)
if len(name) <= 0:
# pydata[1] == 'item'
self.tree.SetItemText(treeItemId, pydata[1])
else:
dostuff()
set_new_pydata()

I found out my own answer
if len(name) <= 0:
event.Veto()

Related

Validate Python Tkinter name entry by clicking button?

So here I have a program which first displays a information message and then you click next and it tells you to input your name before opening up the main window.
INFO ->(next) ENTER NAME ->(next)
When I enter my name in the entry box I want it to be checked that it does not contain 1.numbers and 2.is not blank. under the validate="key" option it means that once I start typing it validates. But rather I want it to only check the name once i press the NEXT button... If not it will open errorbox()
class errorbox():
def __init__(self):
windowError = Tk()
windowError.title("Error")
windowError.geometry('300x400')
error_message = Label(windowError, font=("Bold", 10), justify="left", text="Please enter a valid name")
def clicked1():
description.configure(text="Please enter your name")
nameBox = Entry(windowSplash, width=20, textvariable=name)
nameBox.place(rely=0.5, x=130, anchor=W)
reg = windowSplash.register(validate)
nameBox.config(validate="none",validatecommand=clicked2)
button2 = Button(text="Next", bg="white", width=5, command=lambda:[clicked2(),validate()])
button2.place(rely=0.5, x=300, anchor=E)
button1.destroy()
def validate(input):
if input.isdigit():
print("Invalid name was entered" + input)
errorbox()
return False
elif input is "":
print("No name entered")
errorbox()
return False
else:
return True
def clicked2():
print(name.get(), "logged in...")
windowSplash.destroy()
windowTool = Tk()
windowTool.title("Radial Measurements Calculator Tool")
windowTool.geometry('300x400')
name = StringVar()
windowSplash.mainloop()
Welcome to Stack Overflow Community.
I might have interpreted your question, but please make sure that next time you provide a minimal, reproducible example when you ask.
Here are a couple of things that I have observed.
The validate function takes input as a parameter, so make sure you pass that in the lambda function by lambda input = name.get(): [clicked2(),validate(input)].
By checking input.isdigit() does not guarantee that there might not be numbers after/between characters, so I suggest you to iterate through the string and check for isdigit()/type() or use re module. Also, an efficient way to check for empty string could be if not name.get():.
If you aim to open the new window only after the validation, I suggest you to call clicked2 from the validate function under a condition and not form the next button, because in this case your return form validate isn't used for anything.

Code Processing Too Many Values in PySimpleGUI

I have been making an app which lets the users check-boxes. Depending on what boxes they check it will display different information. I decided to use PySimpleGUI for this project. I made 6 check-boxes and one text input which I want the user to be able to choose between the check-boxes and enter a title of a movie in the text input box. Depending on what check-boxes they select it will display different information based on the movie whose title was entered in the text input.
When I try to process the title value entered in the text input it process all values including the boolean values of the check-boxes. The information my code tries to process is: {0: ;'my input', 'Title': True, 'Year': False...}. I only need to process the my input/the movie title input and not the boolean values of the check-boxes.
Here is an example of my code (for reference I am also using the IMDBPY library to search for movies (which I have made work, the problem is that the id = search[0].movieID line is processing too many values.):
def run_code():
global name
while True:
event, value = window.read()
if event == 'SEARCH':
print(values)
name = str(values)[5:-2]
print('Please wait while your results load...')
search = ia.search_movie(name)
id = search[0].movieID
if values['Title'] == True:
print(movie_title)
I am trying to make my code search for the ID of the film title which would be typed by the user in an input field and than (at the bottom) and print the movie title depending if they have the title checkbox selected. At this point I just get an error saying id = search[0].movieID IndexError: list index out of range) To my understanding id = search[0].movieID is taking too many values (which it is, it is taking in all the values, input and check-boxes) I only want it to take in the text input value.
How should I spread out the values to deal with this issue?
Define keys in your sg elements. Then you can pick from values just the item that you need. For example:
def test():
layout = [[sg.Text('Movie title:'), sg.Input(key='input')],
[sg.Checkbox('Title', key='title'), sg.Checkbox('Reverse title', key='reverse')],
[sg.Button('Search', enable_events=True), sg.Cancel()]
]
window = sg.Window('My Window', layout, finalize=True)
while True:
event, values = window.read()
print(f'event = {event}, values = {values}')
if event in (sg.WINDOW_CLOSED, 'Cancel', 'Exit'):
break
if event == 'Search':
print('Searching now...')
if values['title']:
print(f'Title = {values["input"]}')
if values['reverse']:
print(f'Reverse title = {values["input"][::-1]}')
# any other action as needed
For example, when you check both the checkboxes and click on Search, you'll get
event = Search, values = {'input': 'abcde', 'title': True, 'reverse': True}
Searching now...
Title = abcde
Reverse title = edcba
Now you can be sure that you've got the right title as input by the user.
Except of this, you should check the value you get in search = ia.search_movie(name). For a title not found in the database, you'll probably get None and that's where id = search[0].movieID gets you the IndexError: list index out of range because there is no None[0]. So, you may want to add something like
if search is None:
print('Not found!')
continue

Python Tkinter validation command not working after deleting entry

In my program, the entry widget no longer validates after the delete command has been used on it - the idea is that if it meets a certain requirement, the text in the box is automatically deleted but continues to validate the input.
from tkinter import *
TEXT_TO_MATCH = 'APPLE'
def validate(userinput):
if userinput == TEXT_TO_MATCH:
print(True)
input_box.delete(0, END)
else:
print(False)
return True
window = Tk()
window.title('Delete after validation')
reg = window.register(validate)
input_box = Entry(window, validatecommand=(reg, '%P'), validate='all')
input_box.pack()
window.mainloop()
The entry widget automatically resets the validate option to "none" when you edit the entry widget from within the validation function.
You can re-enable the validation by using after_idle to reset the validate option after control has been returned to mainloop
def validate(userinput):
if userinput == TEXT_TO_MATCH:
input_box.delete(0, END)
input_box.after_idle(lambda: input_box.configure(validate="all"))
return True

Menu for terminal - Up - Down - Enter - Using module keyboard

Just started using python one week ago. At the moment I am trying to code a small program class which creates a menu in a terminal. The idea is to go through the menu using up / down keys. You can select a menu item by pressing enter. I control the keys being pressed using the module "keyboard".
In order to use my class, one has to create an object and add menu items by means of the method "add_menu". The latter mentioned method has two arguments, the first one is used for the name of the menu item, the second one is used to hand over a function, which will be called in case enter was pressed.
In order to check if a key was pressed, I use the method keyboard.on_press from the module keyboard. In case a key was pressed, the method keyboard.on_press executes the method handle_menu of the menu object. The handle_menu method uses a list named "controller" in order to organize the selection of a menu item. Basically, it is just a list like [0,0,1,0]. The element being 1 indicates the currently selected menu item.
Now to my problem: My menu has a menu item "Exit". If this is selected and enter is pressed, I want the whole program to stop. Therefore, if exit was pressed the attribute exit is changed from 0 to 1. In the while loop of my program I always check if object.exit is != 1, if not the program should end. Somehow this does not always work. If I scroll down immediately from the beginning, without pressing enter at other menu items, it works. However, if I press enter several times at other menu items and then go to the exit menu item, the program does not end anymore (or only if I press enter for 10-20 times). I have the feeling that the keyboard.on_press method and the while loop are sometimes uncoupled in the background and run asynchronously? I do not really understand what is going on...
import keyboard #Using module keyboard
import os
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def start_function():
print('Start works')
def save_function():
print('Save works')
def option_function():
print('Option works')
class c_menu:
def __init__ (self):
self.exit = 0
self.menu = []
self.functions = []
self.controller = []
def add_menu(self, menu, function):
self.menu.append(menu)
self.functions.append(function)
if len(self.controller) == 0:
self.controller.append(1)
else:
self.controller.append(0)
def start_menu(self):
os.system('cls' if os.name == 'nt' else 'clear')
for menu_item in range(len(self.menu)):
if self.controller[menu_item] == 1:
print(bcolors.WARNING + self.menu[menu_item])
else:
print(bcolors.OKBLUE + self.menu[menu_item])
def handle_menu(self, event):
os.system('cls' if os.name == 'nt' else 'clear')
if event.name == 'down':
if self.controller.index(1) != (len(self.controller) - 1):
self.controller.insert(0,0)
self.controller.pop()
elif event.name == 'up':
if self.controller.index(1) != 0:
self.controller.append(0)
self.controller.pop(0)
for menu_item in range(len(self.menu)): #printing all menu items with the right color
if self.controller[menu_item] == 1:
print(bcolors.WARNING + self.menu[menu_item])
else:
print(bcolors.OKBLUE + self.menu[menu_item])
if event.name == 'enter':
if self.functions[self.controller.index(1)] == 'exit':
self.exit = 1
return
self.functions[self.controller.index(1)]()
main_menu = c_menu()
main_menu.add_menu('Start', start_function)
main_menu.add_menu('Save', save_function)
main_menu.add_menu('Option', option_function)
main_menu.add_menu('Exit', 'exit')
main_menu.start_menu()
keyboard.on_press(main_menu.handle_menu)
while main_menu.exit != 1:
pass
I think I understood the problem. The program is actually ending properly, however, the last "enter" pressed is still in a kind of buffer (or something similar) and after the end of program, the terminal command "python menu.py" is executed again and again (it goes so fast that it looks like the program did not end). Unfortunately, I do not really understand why this is happening.
My solution so far, I use "keyboard.send('ctrl+c')" at the very end of my program (after the while loop). This prevents the terminal to re-execute the command "python menu.py" again.

How to add 'backspace' event to easygui multenterbox fileds?

I am using easygui multenterbox so as to give users the option to edit specific information out of data being copied. My only problem is that pressing 'backspace' results in '\b' being added to the string instead of erasing a character.
Has anybody figured out a way to add a <backspace> event to the field within the multenterbox?
I have used this event in the past on tkinter "Entry" and "Text".
sample code:
msg = "Enter name"
title = "New name"
fieldName = ['name']
newTitle= 'NewName'
fieldValue = [newName]
fieldValue = multenterbox(msg,title, fieldName,fieldValue)
# make sure that none of the fields were left blank
while 1: # do forever, until we find acceptable values and break out
if fieldValue == None:
break
errmsg = ""
# look for errors in the returned values
for i in range(len(fieldName)):
if fieldValue[i].strip() == "":
errmsg = errmsg + '"{}" is a required field.\n\n'.format(fieldName[i])
if errmsg == "":
# no problems found
print ("Reply was:", fieldValue)
break
else:
# show the box again, with the errmsg as the message
fieldValue = multenterbox(errmsg, title, fieldName, fieldValue)
The above code snippet is showing the functionality that I already have, and works. My program will open a multenterbox window with the value of NewTitle which could be edited by the user. How do I control the editable entry in the multenterbox so that it supports backspace?
Thanks,
MMH

Categories

Resources