I have code to display rows and columns.
I wanted to get the row and column in shell,if I click with my mouse over the specified location consisting like R0/C0 in the GUI
My coding:
import Tkinter
root = Tkinter.Tk( )
for r in range(3):
for c in range(4):
Tkinter.Label(root, text='R%s/C%s'%(r,c),
borderwidth=1 ).grid(row=r,column=c)
root.mainloop( )
If i click my mouse over R2/C2 in the GUI,then it should display the output in shell as R2/C2
Please help me on how to attain this!
import Tkinter
root = Tkinter.Tk()
def handle_click(text):
print text
for r in range(3):
for c in range(6):
text = 'R%s/C%s'%(r,c)
label = Tkinter.Label(root, text=text, borderwidth=1 )
label.grid(row=r,column=c)
label.bind("<Button-1>", lambda e, text=text:handle_click(text))
root.mainloop()
Related
I'm creating a Gui with Tkinter with a lot of Entry widgets. However, I don't like the fact of clicking on Tab button to go from one entry to another. I would like it to be the Enter key that does this. Is there a function to make it happen ?? here is the list of all entries:
```python
entries = [self.entMath1, self.entMath2, self.entFran1,
self.entFran2, self.entSvt1, self.entSvt2, self.entEps1,
self.entEps2, self.entHg1, self.entHg2, self.entPc1,
self.entPc2, self.entAng1, self.entAng2, self.entPhi1,
self.entPhi2, self.entM1, self.entM2, self.entM3,
self.entM4]
for i in range(len(entries_1) - 1):
entries_1[i].bind("<Return>", lambda e: entries_1[i].focus_set())
```
all these entries are placed with place() method.
You can bind the <Return> key event on the Entry widget. Then in the bind callback, get the next widget in the focus order by .tk_focusNext() and move focus to this widget:
import tkinter as tk
root = tk.Tk()
for i in range(10):
e = tk.Entry(root)
e.grid(row=0, column=i)
e.bind('<Return>', lambda e: e.widget.tk_focusNext().focus_set())
root.mainloop()
Have a read of this answer binding enter to a widget for the difference between <Return> and <Enter>
From that, you can build something like
import tkinter as tk
top = tk.Tk()
frm = tk.Frame(top)
frm.pack()
# List of entries
seq = [None]
next = []
entmax = 5
for ii in range(entmax):
ent = tk.Entry(frm)
ent.pack(side=tk.LEFT, padx=5, pady=5)
seq.append(ent)
next.append(ent)
# Now set the entries to move to the next field
for ix in range(1, entmax, 1):
seq[ix].bind('<Return>', lambda e: next[ix].focus_set())
top.mainloop()
I want to create a column of 10 buttons and have them numbered in order.
Is there a simpler way to do it using for loops that I can add the text to these buttons rather than coding out each button line by line?
This is what I have but it only puts the the number 10 text in each button?
I want each button numbered starting from 1 thru 10.
from tkinter import *
root = Tk()
#Create Buttons
def create_buttons():
number_text = []
for nums in range(0,10):
number_text.append(str(nums))
for col in range(10):
for num in number_text:
buttons = Button(root, font=30, text = num, padx=15, pady=10)
buttons.grid(row=0, column=col)
create_buttons()
root.mainloop()
This is my solution for your problem,
from tkinter import *
root = Tk()
#Create Buttons
def create_buttons():
for num in range(1,11):
buttons = Button(root, font=30, text =str(num), padx=15, pady=10)
buttons.grid(row=0, column=num)
create_buttons()
root.mainloop()
Actually, you can solve this one easily. Every time you should try to reduce the complexity of the program. Don't think much. So I supposed this code much easier to solve your problem. Here I using only one loop and that loop I use for the given name for the button and also the given column number. You using the same parameters for the Button widget and grid manipulation. When we use another name for the button you should want to create a list. That code example is given below,
from tkinter import *
root = Tk()
button_name=[f'number_{i}' for i in range(10)]
#Create Buttons
def create_buttons():
for num in range(1,11):
buttons = Button(root, font=30, text =str(button_name[num-1]), padx=15, pady=10)
buttons.grid(row=0, column=num)
create_buttons()
root.mainloop()
It might be because the 1st button gets overlapped by 2nd ... so on to 9th button. So, you see 9 on top, which hides other buttons.
Try a different way -
from tkinter import *
root = Tk()
#Create Buttons
def create_buttons():
number_text = [*range(1,10+1)] # Or you could just do this in your way
for col in range(10):
buttons = Button(root, font=30, text = number_text[col] , padx=15, pady=10)
buttons.grid(row=0, column=col)
create_buttons()
root.mainloop()
This will take the index number_text[col] and show the buttons.
root = Tk()
def select(n):
lst.append(n)
for i in range(4):
Button(root, text="button"+str(i), command=lambda x="button"+str(i): select(x,x)).grid(row=0, column=i)
root.mainloop()
is there a way where I can call this button to re-configure?
You can access all widgets using root.children, this will return a dictionary of all widgets as keys and a reference to those widgets as values.
Here is a demonstration that changes your coded button background colors to red and displays widget.keys() data in shell.
"""
import tkinter as tk
root = tk.Tk()
for i in range(4):
tk.Button(root, text = f"button{i}").grid(row = 0, column = i, sticky = tk.NS)
# collect all objects
buttons = root.children
for k, v in buttons.items():
print(k, v.keys())
# Arbitrarily change colors
buttons[k]['background'] = "red"
# Change text in buttons
buttons[k]['text'] = "New name"
root.mainloop()
from tkinter import * // Libraries imported
import tkinter as tk
from tkinter import simpledialog
ROOT = tk.Tk()
strong textROOT.withdraw()
root = Tk()
s=[] //empty list to append entry values
i=0 // to iterate over for loop
for y in range(5):
r= Label(root, text="file_"+str(y)).grid(row=i) //5-labels created using for loop
i=i+1
i=0
for y in range(5):
r=("file_"+str(y)) //5 entry boxes created using for loop
r = Entry(root)
r.grid(row=i , column=1)
i=i+1
def getInput():
for y in range(5): //entry value is stored
r = ("file_"+str(y))
b = r.get()
s.append(b)
root.destroy()
Button(root, text = "submit",command = getInput).grid(row = 5, sticky = W)
//click box 'submit' is created to store values into empty list 's'//
mainloop() //code ends
//The code is showing error : AttributeError: 'str' object has no attribute 'get
//I am not able to store my entry values into an empty list S and later retrieve the entry values of that list.
r = ("file_"+str(y)) followed by b = r.get() won't magically retrieve the contents of the widget. You need to store your Entry widgets in a container like a list.
You could also get rid of the 2nd loop - why don't create both Label and Entry in the same loop?
import tkinter as tk
root = tk.Tk()
entries = []
for y in range(5):
tk.Label(root, text="file_"+str(y)).grid(row=y,column=0)
r = tk.Entry(root)
r.grid(row=y,column=1)
entries.append(r)
def getInput():
print ([ent.get() for ent in entries])
tk.Button(root, text = "submit", command = getInput).grid(row = 5, sticky = "w")
root.mainloop()
I am trying to create a column of buttons which when they are pressed, the grid that down a row show pop up another two buttons.
for i in tlist:
opl.append(Tk.Button(self.frame, width = 12 , text=i[3],command = lambda:self.pressedop(i[2], a)))
opl[a].grid(row=a, column=0, columnspan=2)
a = a + 1
That successfully create a column of buttons, with text(i[3]) correctly shown, but when a button is pressed, i[2] will be last button's info, and a will be last button's row +1.
Is there a way which I can pass the i[2] and it's own grid_info down?
I don't understand your question, but you can use widget.grid_forget() to remove a widget from the geometry manager. If you then want it in a different row, just widget.grid() with the new row and column. A simple example
try:
import Tkinter as tk ## Python 2.x
except ImportError:
import tkinter as tk ## Python 3.x
from functools import partial
opl = []
top = tk.Tk()
def move_it(num):
for ctr in range(num, 5):
opl[ctr].grid_forget()
opl[ctr].grid(row=6, column=ctr)
for but_num in range(5):
opl.append(tk.Button(top, width = 12 , text=str(but_num),
command = partial(move_it, but_num)))
opl[-1].grid(row=but_num, column=0, columnspan=2)
top.mainloop()