Hi people is it possible to insert Label widget into Listbox in tkinter instead of some str value and attach to it a scrollbar?
Based on the documentation online, No
The listbox can only contain text items...
for further details on the ListBox: http://effbot.org/tkinterbook/listbox.htm
Don't ask me to explain it because I really don't understand it yet, but this should work:
import tkinter as tk # python3
root = tk.Tk()
myList = tk.Listbox(root)
myText = "A list item"
myList.insert(tk.END, myText)
myList.pack()
lbl = tk.Label(myList, text=myText, anchor="w", font=("Helvetica", "24"))
lbl.pack(side="top", fill="x", anchor="w")
root.mainloop()
Related
My problem: I can't display the value to Text widget when I select a row.
how can I display a value inside the Text widget from the selected row?
I tried using textvariable attribute in Text widget but its not working.
Use insert method of the Text widget. Minimal example:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
text.pack()
def add_text():
# text.delete(1.0, tk.END) # Uncomment if you need to replace text instead of adding
text.insert(tk.END, f"Some text\n")
tk.Button(root, text="Add text", command=add_text).pack()
root.mainloop()
I have been struggling to get the text in the border of a tk.LabelFrame to change when I open a new text file inside the frame. I have been using essentially the same code as I use elsewhere to change a tk.Label.
I just came across this in some documentation:
"When a new labelframe is created, it has no default event bindings: labelframes are not intended to be interactive."
The last phrase is fairly definitive, but the use of the word 'default' makes me wonder if there is a way of achieving this.
So, my question is, can it be done?
Thanks to #Bryan Oakley who mentioned the magic word 'configure'which got me thinking along different lines and I came up with this solution:
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('LabelFrame Demo')
lf = ttk.LabelFrame(
root,
text='text to change')
lf.grid(column=0, row=0, padx=20, pady=20)
def file_1_label():
my_text = 'editing file 1'
lf.configure(text='editing FILE 1')
def file_2_label():
my_text = 'editing file 2'
lf.configure(text='editing FILE 2')
button1 = tk.Button(lf, text='File 1')
button1.grid(row=0, column=0)
button1.configure(command=file_1_label)
button2 = tk.Button(lf, text='File2')
button2.grid(row=0, column=2)
button2.configure(command=file_2_label)
root.mainloop()
I would like to print each items in list as TKinter label using a for loop when I click a button. Below is the code I have, however it doesn't output anything.
The result I am looking for is in first instance the label should print "111111" and in next instance "222222".
from tkinter import *
def get_searchid():
search_id = ["1111111",'222222']
for search in search_id:
l1 = Label(root, text=search)
l1.pack
root = Tk()
button1 = Button(root, text="Search Me", command=get_searchid)
button1.place(x=50, y=50)
button1.pack
root.mainloop()
Any ideas on how I can achieve this or what I am doing wrong?
I am using a ttk.Treeview widget to display a list of Arabic books. Arabic is a right-to-left language, so the text should be aligned to the right.
The justify option that is available for Label and other ttk widgets does not seem to work for Treeview.
Does anyone know how to do this?
The ttk.Treeview widget has an anchor option you can set for each column. To set the anchor of a column to the right side use:
ttk.Treeview.column(column_id, anchor=Tkinter.E)
In addition to the #fhdrsdg answer, here you have a simple example of use:
# for python 3
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
def show_info():
messagebox.showinfo("More info", "First column represents the subject" \
" and the second represents its corresponding " \
"current number of tagged questions on Stack Overflow.")
root = tk.Tk()
tree = ttk.Treeview(root, columns=("Tags"), height=6)
subjects = {"Tkinter": "8,013",
"Python": "425,865",
"C++": "369,851",
"Java": "858,459"}
for subject in subjects.keys():
tree.insert("", "end", text=subject, values=(subjects[subject]))
tree.column("Tags", anchor="e")
tree.pack(fill="both", expand=True)
informer = tk.Button(root, text="More info", command=show_info)
informer.pack(side="bottom")
root.mainloop()
If you need more help on how to use ttk.Treeview widgets, have a look at this reference by The New Mexico Tech or this tutorial at TkDocs.
Question
I'm trying to make a 'ttk Label' which a) is 20 pixels high by 300 pixels wide, b) is scrollable (in this case horizontally), and c) uses the simplest code possible within reason (except for the fact that the text and scrollbar are both within a frame*). I've found stackoverflow to be helpful in describing the processes I need to go through (put the label in a frame, put the frame in a canvas, put the scroll bar next to or underneath the canvas and 'bind' them together somehow), but despite looking at a fair few docs and stackoverflow questions, I can't figure out why my code isn't working properly. Please could someone a) update the code so that it satisfies the conditions above, and b) let me know if I've done anything unnecessary? Thanks
*the frame will be going in a project of mine, with text that is relevant
Current code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
myframe_outer = ttk.Frame(root)
mycanvas = tk.Canvas(myframe_outer, height=20, width=300)
myframe_inner = ttk.Frame(mycanvas)
myscroll = ttk.Scrollbar(myframe_outer, orient='horizontal', command=mycanvas.xview)
mycanvas.configure(xscrollcommand=myscroll.set)
myframe_outer.grid()
mycanvas.grid(row=1, sticky='nesw')
myscroll.grid(row=2, sticky='ew')
mycanvas.create_window(0, 0, window=myframe_inner, anchor='nw')
ttk.Label(myframe_inner, text='test ' * 30).grid(sticky='w')
root.mainloop()
Edit:
Current result
Answer
Use a readonly 'entry' widget - it looks the same as a label, and doesn't need to be put in a canvas.
Code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
mytext = tk.StringVar(value='test ' * 30)
myframe = ttk.Frame(root)
myentry = ttk.Entry(myframe, textvariable=mytext, state='readonly')
myscroll = ttk.Scrollbar(myframe, orient='horizontal', command=myentry.xview)
myentry.config(xscrollcommand=myscroll.set)
myframe.grid()
myentry.grid(row=1, sticky='ew')
myscroll.grid(row=2, sticky='ew')
root.mainloop()
Result