Setting tkinter Text yview / Text view area - python

I would like to set the tkinter Text box's view area / yview to a specific area. How do I do this? I know that one can retrieve the current yview but have been unsuccessful in setting it to a desired value. I know that it can be set to the desired value because I am getting the yview, destroying the Text box, and then recreating the text box. From there I would like to set the view area to what it was before but am having trouble. What should I do?
Example yviews:
(0.0, 1.0)
(0.07407407407407407, 1.0)
Code:
#Define the Text widget
t = Text(window)
t.pack()
for i in range(100):
t.insert(END, "{}\n".format(i))
def get_y():
global y
y = t.yview()
b = Button(window, text="get y", command=get_y)
b.pack()
b1 = Button(window, text="set y", command=lambda: t.yview(MOVETO, y[0]/y[1]))
b1.pack()
The code above gets the user very close but it always goes a little up or down and isn't quite right. What should I do to fix it? Can this be done similarly with xview?

I figured it out. We can get the yview with t.yview() but we can't just insert a yview value and expect it to use it. Instead, we must use t.yview(MOVETO, yview[0]). This gives it a value it can work with and goes to about the correct position
NOTE:
The same can be done with xview but use xview[1] instead

Related

Position the button in right place Tkinter

How can I position the two button close to eachother. I am getting the output as shown in the picture below.
_scrape_btn = ttk.Button(_mainframe, text='Scrape!', command=save)
_scrape_btn.grid(row=2, column=0)
_compress_btn = ttk.Button(_mainframe, text='Compress!', command=compress)
_compress_btn.grid(row=2, column=1)
Looking at your image, I can tell that your URL and Content LabelFrames have been put into row=0, column=0 and row=1, column=0 respectively. So you should now realise that the width of the first column is as large as the labelframe. Hence your 2nd button starts where the 1st column ends since it on the 2nd column.
What you can do is add columnspan=2 in both the labelFrames.
Now by default the buttons will be center aligned. To bring them close together, you need to add sticky='e' in the grid command of the 1st button (Scrape) and sticky='w' in the grid command of the 2nd button (Compress).

Certain label wont work with .grid() function from tkinter

It seems that I explained my problem very terribly, but I have a problem with the grid function on my label. The label does show up but, I cant change the row/column of it or do any function inside of the brackets.
I put the code of how to replicate the problem there. Putting anything inside of the .grid() brackets does nothing as stated earlier
from tkinter import *
root = Tk()
#To actually see that it does not work
root.geometry("800x600")
Var = StringVar()
Label = Label(root, textvariable=Var)
#Location of problem, adding stuff into the brackets does not change anything. For example: sticky = "ne"
Label.grid()
Var.set("Ha")
root.mainloop()
To get the label to show up on the right side, you can specify the number of columns in the grid and then place the label in the last column. For example, try adding this just after your "#Location of problem" comment:
cols = 0
while cols < 4:
root.columnconfigure(cols, weight=1)
cols += 1
Label.grid(column=3)

Why can't the contents in these three entries change simultaneously?

Firstly let's look at this program:
def entry_simutaneously_change():
from Tkinter import *
root=Tk()
text_in_entry.set(0)
Entry(root, width=30, textvariable= text_in_entry).pack()
Entry(root, width=30, textvariable= text_in_entry).pack()
Entry(root, width=30, textvariable= text_in_entry).pack()
root.mainloop()
The contents in these three entries can change simultaneously. If I change the value of any of them, the other two would change at the same time. However, for the following program:
def entry_simutaneously_change():
from Tkinter import *
root=Tk()
text_in_entry_list=[IntVar() for i in range(0,3)]
text_in_entry_list[0].set(0)
text_in_entry_list[1].set(text_in_entry_list[0].get() ** 2)
text_in_entry_list[2].set(text_in_entry_list[0].get() ** 3)
Entry(root, width=30, textvariable= text_in_entry_list[0]).pack()
Entry(root, width=30, textvariable= text_in_entry_list[1]).pack()
Entry(root, width=30, textvariable= text_in_entry_list[2]).pack()
root.mainloop()
When I change the content in the first entry, the contents in the other two do not change. Why?
In the first program, you have one source of data, text_in_entry. You could consider it as one box where you're placing a single value that's read by each Entry.
In the second program, you have three sources of data, text_in_entry[0, 1, and 2]. The lines that set the initial value are called only once. It's like you have three boxes where data is placed; to set the initial values, you do look at the value inside the first, but there is no association between the three.
If you would like to achieve the same type of result as with the first program (when the three entries update simultaneously) then you will need to bind on an event. I note that Tkinker does not have an on-change style event but there are various hacks, however you could bind to the FocusOut event. The general idea is to ask for a function to be called when a change occurs to an entry (binding a callback). Inside that function you update the other two values based on the new value in the entry that was changed.

My Variable will not change out side the definition

I wrote my first py code. This code to create a lotto number generator.The problem is that my variable nums will not change. Please help me.
I understand this is not as good as it can be so please tell me how to improve, any comments will be appreciated.I wanted the random numbers to come up in the ladle so i could press the submit button and 5 new numbers to come up.The problem is my veritable "nums" wont change.Thank you for helping.
import random
from tkinter import *
#TK()
window = Tk()
window.title("Lottery Nummber Generator")
#Def Click
def click():
global nums
global numsgen
numsgen = random.sample(range(1, 49), 5)
nums = " ".join(str(x) for x in numsgen)
print(nums)
numsgen = random.sample(range(1, 49), 5)
nums = " ".join(str(x) for x in numsgen)
#Fake just to make it look nice
Label(window,text="").grid(row=1, column=0,sticky=W)
Label(window,text="").grid(row=2, column=1,sticky=W)
Label(window,text="").grid(row=2, column=3,sticky=W)
#Submit button
Button(window, text="Submit", width=5,command=click).grid(row=3, column=1, sticky=W)
#Label
group = LabelFrame(window, text="Lottery Numbers:", padx=5, pady=5,fg="dark orange")
group.pack(padx=10, pady=10,)
group.grid(row=2, column=1,sticky=W)
w = Label(group, text=nums)
w.pack()
mainloop()
To get a label to update, you must do one of two things:
Associate a StringVar with the label. When you update the variable (via its set method), any labels associated with it will automatically change. This is a great solution if more than one widget needs to display the same value.
Directly configure the label. This requires that you save a reference to the widget(s) so that you can later reconfigure them. For example, you could put w.configure(text=nums) in your click function. This solution is a bit simpler than using a StringVar, simply because it requires one less object.

Finding widgets on a grid (tkinter)

Using the tkinter module, suppose I create a grid with 50 button widgets and each of those widgets has different text. I need to be able to specify some way of typing in a row and column so I can get that widget's text at that location.
For example, if I need the widget's text at the third row in the second column of the grid. I've searched the docs but that tells me how to get info about widgets, when I need info about the grid.
There's no need to create your own function or keep a list/dictionary, tkinter already has a built-in grid_slaves() method.
It can be used as frame.grid_slaves(row=some_row, column=some_column)
Here's an example with a grid of buttons showing how grid_slaves() retrieves the widget, as well as displaying the text.
import tkinter as tk
root = tk.Tk()
# Show grid_slaves() in action
def printOnClick(r, c):
widget = root.grid_slaves(row=r, column=c)[0]
print(widget, widget['text'])
# Make some array of buttons
for r in range(5):
for c in range(5):
btn = tk.Button(root, text='{} {}'.format(r, c),
command=lambda r=r, c=c: printOnClick(r, c))
btn.grid(row=r, column=c)
tk.mainloop()
You got a previous answer relative to a method to save button objects in a dictionary in order to recover them using their (column, row) position in a grid.
So if self.mybuttons is your dictionary of lists of buttons as described in previous answer, then you can get the text at position row, col as this:
abutton = self.mybuttons[arow][acolumn]
text_at_row_col = abutton["text"]
On the other hand, if what you need is to get the text from the button callback:
button.bind("<Button-1>", self.callback)
then you can get the button text from the event, you do not need to know its row/col position, only to press it:
def callback(self, event):
mybutton = event.widget
text_at_row_col = mybutton["text"]

Categories

Resources