So i have coded a keyboard in tkinter, and i am trying to create a game where the user has to click the key corresponding to the displayed character. Once they click the correct key the character changes to another character. I want to have a certain number of characters that once the user has clicked them all they win.
At the moment I have the code working so when they click they right key the character changes. But i cant seem to work out how to keep track of when they have finished clicking all the right keys.
All the keys have this command
new_but = Button(button_frame, text=key, command=lambda x=key: letter_check(x,target))
and here is the code for the command
def clear_data(String):
String.set("")
def show_data(string):
data.set(string)
def letter_check(string, target):
if data.get() == string:
data.set("")
target = next_target(target)
def next_target(block):
new_block = list(block)
letter = random.choice(new_block)
new_block.remove(letter)
show_data(letter)
block = str(new_block)
print(new_block)
return block
target = "abcdef"
n = 3
where the show_data and clear_data methods just change the display character. I know that in the command I am always going to be using the original target string "abcdef"
I want it to work so at the start I have the target string "abcdef", then the system will randomly pick one of the character in the target string say it chooses "b" then I want the target string to now be "acdef" then when the corresponding key ('b') is clicked, again it chooses another random char say "e" then the target string should be "acdf" and so on until the target string is empty then the game is won. I just don't know how to keep track of the modified target string in the methods. I am fairly new to tkinter so not sure if I can have a "main" method to keep track of everything? Any help is appreciated
Related
I have an entry widget as such:
...
entry1=Entry(frame1,font=("Verdana",10),width=20)
entry1.grid(row=0,column=0,rowspan=2,pady=10,padx=10,ipday=5)
and when I type a long string within in,
it crops out the last letter such that I can't even bring it into view using the right arrow key
(12039201901--71230110)
but if I add one more character then I can use the right arrow key to bring it into view
How do I fix this issue?
Ideally the user will type in some text into a scrolledtext widget. When this is done, the user will go into setting mode and select the key words. These key words will then go into an array.
I have managed to create the gui and the 'settings' mode but I do not know how to get it so when the word is clicked the word selected will be added to a list. Here is what I have started with to decide if it is in setting mode or not as well as binding the _on_click_ to the textbox.
ArticleTextBox.bind("<ButtonRelease-1>", _on_click)
def _on_click(event):
state = str(namebutton['state'])
if state != 'disabled':
Thanks in advance for your help and if you have any questions feel free to ask them :)
I saw answer to your previous question Highlighting words and then unhighlighting using tkinter and to get clicked text you can do:
def _on_click(self, event):
word = self.text.get("insert wordstart", "insert wordend")
print(word)
so now you can add it to your list.
im making a grocery list app with tkinter but im having trouble where as telling the computer to reject a repeated grocery. my code so far looks like this
def getlis():
global ind,lastite,ovelim,curite
curite=groent.get()
for x in range(0,len(shotup)):
if curite==shotup[x]:
mylist.insert(ind,shotup[x]+'\t $%.2f'%(pritup[x]))
lastite=shotup[x]
if curite==lastite and ovelim>1:
mylist.insert([ind],'error, already inputted')
t.sleep(1)
mylist.delete(ind)
ovelim+=1
ind+=1
i want the computer to first acept that the item i entered into groent matches up with one of the items in the tuple shotup then print it into a listbox, then recording it into a variable called lastite, the last thing you put on the list, ovelim is just a variable that helps keep track in a way. when ovelim>1 and lastite==curite i want the program to return whats in the second if statement, but all im getting is just a continued recording into my list, ive tried reorganizing, true false and mor ebut nothing seems to work
I'm writing a code that searches a .json with data about all the Magic the Gathering cards according to many possible different criteria.
It works fine except that whenever a card has characters such as "-" in its text, my code will ignore that text entirely. It is as its text field was blank.
At some point in the code, the cards' texts are appended to a list, and maybe that's where the problem lies, because i made a simple .py file to test something different, and i can get the text.
for i in data:
if data[i]["name"].find(" some name* ")!=-1:
print data[i]["text"]
This is working actually. it prints the text of the card flawlessly.
however, when i switch this to
for i in data:
if data[i]["name"].find(" some name* ")!=-1:
somelist.append(data[i]["text"]
print somelist
i will find the same issue again. i will get the text of cards that don't have characters such as "-" on them.
EDIT: This is an example of text that python will ignore:
"+3: Destroy target noncreature permanent.\n−2: Gain control of target creature.\n−9: Nicol Bolas, Planeswalker deals 7 damage to target player. That player discards seven cards, then sacrifices seven permanents."
This â^'9 should be translated to "-".
Another text example that has been ignored every single time is this:
"Target creature gets -1/-1 until end of turn.\nMorbid — That creature gets -13/-13 until end of turn instead if a creature died this turn."
I am using an onscreen keyboard to type data for a tkinter based gui.
I was able to use the entry field to enter, edit data through the onscreen keyboard such as getting current cursor position and length of the string.
temp = self.entry_label.get()
length_string=len(temp)
cursor_position = self.entry_label.index(INSERT)
But I wish to do the same for a Text widget. I could get the text for Text widget using get() method and its length but cannot get the current mouse cursor position.
temp=self.new_text.get(1.0, END)
cursor_position = self.new_text.index(INSERT)
Acutally it works and i am able to add character to that poisition , but after adding character cursor goes back to origional position , i.e last character
maybe This works? Else maybe text_widget.index(Tkinter.INSERT) is what should work.
Although the other answer is correct and worked I believe tk.Current is the meta answer.
"tk.INSERT"
as provided by this reference.
https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/text-index.html