TKinter - Clear text widget - text

I am inputting a random number from 1 to 25 into a text box on button press.
Every time i press the button, i want the text box to be emptied before the new number is stored - but putting self.outputbox.delete("1.0") into the button command only deletes the first character. i tried replacing 1.0 by something obvious like 2.0, but it never deleted both characters. At the moment, i am doing a very dirty solution by just calling self.outputbox.delete("1.0") twice - but i want to know how to delete the entire content of the text box.
this is the part:
def change_button_color(self):
randomcolor = self.get_random_color()
randombutton = self.random_button()
for z in range(0,1):
self.buttons['button{}'.format(randombutton)].config(bg=randomcolor)
for i in range(0,40):
self.outputbox.delete("1.0")
self.outputbox.insert("1.0","Button" + str(randombutton) + " " + "has the color" + " " + randomcolor)
as you can see, i currently have a rather dirty solution to clear the box

Do this
self.outputbox.delete("1.0", self.END)
it will clear all the text widget
And if you imported tkinter as tk do this
self.outputbox.delete("1.0", self.tk.END)

Related

Sublime Text selection issue

In my Sublime Text (v3), there seems to be some issue suddenly with selection. So assume I have the following code;
const testvar = '123';
Earlier, if I double clicked on either const Or testvar, it just selected that. However for some reason, it is now selecting both const & testvar
Earlier, if I had my cursor at the start of line and did Ctrl + Right arrow, it would take me to start of next word i.e. testvar. But now, it seems to be hoppinh over 2 words and cursor goes to =
Not sure if some setting got changed unintentionally.

I need help in Python with displaying the contents of a 2D Set into a Tkinter Textbox

Disclaimer: I have only begun to learn about Python. I took a crash course just to learn the very basics about a month ago and the rest of my efforts to learn have all been research thru Google and looking at solutions here in Stack Overflow.
I am trying to create an application that will read all PDF files stored in a folder and extract their filenames, page numbers, and the contents of the first page, and store this information into a 2D set. Once this is done, the application will create a tkinter GUI with 2 listboxes and 1 text box.
The application should display the PDF filenames in the first listbox, and the corresponding page numbers of each file in the second listbox. Both listboxes are synched in scrolling.
The text box should display the text contents on the first page of the PDF.
What I want to happen is that each time I click a PDF filename in the first listbox with the mouse or with up or down arrow keys, the application should display the contents of the first page of the selected file in the text box.
This is how my GUI looks and how it should function
https://i.stack.imgur.com/xrkvo.jpg
I have been successful in all other requirements so far except the part where when I select a filename in the first listbox, the contents of the first page of the PDF should be displayed in the text box.
Here is my code for populating the listboxes and text box. The contents of my 2D set pdfFiles is [['PDF1 filename', 'PDF1 total pages', 'PDF1 text content of first page'], ['PDF2 filename', 'PDF2 total pages', 'PDF2 text content of first page'], ... etc.
===========Setting the Listboxes and Textbox=========
scrollbar = Scrollbar(list_2)
scrollbar.pack(side=RIGHT, fill=Y)
list_1.config(yscrollcommand=scrollbar.set)
list_1.bind("<MouseWheel>", scrolllistbox2)
list_2.config(yscrollcommand=scrollbar.set)
list_2.bind("<MouseWheel>", scrolllistbox1)
txt_3 = tk.Text(my_window, font='Arial 10', wrap=WORD)
txt_3.place(relx=0.5, rely=0.12, relwidth=0.472, relheight=0.86)
scrollbar = Scrollbar(txt_3)
scrollbar.pack(side=RIGHT, fill=Y)
list_1.bind("<<ListboxSelect>>", CurSelect)
============Populating the Listboxes with the content of the 2D Set===
i = 0
while i < count:
list_1.insert(tk.END, pdfFiles[i][0])
list_2.insert(tk.END, pdfFiles[i][1])
i = i + 1
============Here is my code for CurSelect function========
def CurSelect(evt):
values = [list_1.get(idx) for idx in list_1.curselection()]
print(", ".join(values)) ????
========================
The print command above is just my test command to show that I have successfully extracted the selected item in the listbox. What I need now is to somehow link that information to its corresponding page content in my 2D list and display it in the text box.
Something like:
1) select the filename in the listbox
2) link the selected filename to the filenames stored in the pdfFilename 2D set
3) once filename is found, identify the corresponding text of the first page
4) display the text of the first page of the selected file in the text box
I hope I am making sense. Please help.
You don't need much to finish it. You just need some small things:
1. Get the selected item of your listbox:
selected_indexes = list_1.curselection()
first_selected = selected_indexes[0] # it's possible to select multiple items
2. Get the corresponding PDF text:
pdf_text = pdfFiles[first_selected][2]
3. Change the text of your Text widget: (from https://stackoverflow.com/a/20908371/8733066)
txt_3.delete("1.0", tk.END)
txt_3.insert(tk.END, pdf_text)
so replace your CurSelect(evt) method with this:
def CurSelect(evt):
selected_indexes = list_1.curselection()
first_selected = selected_indexes[0]
pdf_text = pdfFiles[first_selected][2]
txt_3.delete("1.0", tk.END)
txt_3.insert(tk.END, pdf_text)

Typing/Entering a value into a text field - Python 2.7

I am reading cell values from an excel sheet using xlrd, clicking into a text box, and I want to the enter the excel values into that text box. Here's the bit of my code that matters;
#move the cursor to a location
#-------------------------------------------------------------------------------
win32api.SetCursorPos((200,100))
#right click
#-------------------------------------------------------------------------------
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
print "Click."
#enter the room name(HERE'S WHERE I NEED HELP)
#-------------------------------------------------------------------------------
myString = (sheet.cell(0,0).value)
print myString
I know 'print myString' will just print it in the shell, but I've left it for clarification purposes.
Any suggestions on how to enter the value into the box?
P.S. After my script double clicks, the text box appears, and the text in it is highlighted and ready for typing to begin.
Thanks,
Mason

Adding a new Text to textView instead of setting the text in java FX

I want the textview to add more line of code instead of changing/seting the text. The code below get called by a loop. The problem is only the last player's average score gets shown because the code changes the text per increment in loop and doesn't add the text below the previous text. I want each players average score to be displayed on the next line.
int average = playerTotalScore.get(player)/getRowSize();
textArea.setText(player + " average score: " + average);
Read out the current text value, append and set it:
String content = textArea.getText() + "\n" + player + " average score: " + average;
textArea.setText(content);

How to Change text color?

Hi
I am using following code to append text and I want to change the color of newly appended text how is it possible
if (strMessage.IndexOf("pvt|") == -1)
{
string[] temp = strMessage.Split(new string[] {"&^:^&"}, tringSplitOptions.None);
strMessage = temp[0] + "(" + DateTime.Now.ToString("HH:mm tt") + ")" + ":" + emp[1];
txtLog.AppendText(strMessage + "\r\n");
}
Please help..
Where is the text being displayed? It appears that it goes to a TextBox, but that's just an assumption.
Changing the color of individual chunks of text within a normal TextBox is not possible. You can change the entire TextBox's forecolor by setting its ForeColor property.
txtLog.ForeColor = Color.Red;
To change pieces of it individually, then you will need to use a different textbox, such as shown in the answer here.
Assuming that txtLog is a text file you would have to encode ascii control characters into stream and have an editor that read them. This link might help.

Resources