below is my code for a basic tkinter scale:
root = tk.Tk()
root.geometry('150x75')
root.resizable(width=False, height=False)
root.eval('tk::PlaceWindow . center')
v1 = tk.IntVar()
ttk.Scale(root, from_=0, to=100, orient='horizontal', variable=v1).pack(pady=20)
root.mainloop()
below is the attached output :
why is the slider value not showing above the slider ?
I would very much appreciate if someone could point out the error with the code.
The tkinter.ttk version of Scale does not support showing the value.
But the tkinter version of Scale DOES support showing the Scale value, by default. The showvalue option defaults to 1. You can set it to 0 to hide the value if so desired.
There is no error in your code. I think the answer is "because it's not designed to". There is no mention in the documentation of the ttk scale widget that it shows the value.
Try something like this:-
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('Slider Demo')
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=3)
# slider current value
current_value = tk.DoubleVar()
def get_current_value():
return '{: .2f}'.format(current_value.get())
def slider_changed(event):
value_label.configure(text=get_current_value())
# label for the slider
slider_label = ttk.Label(root, text='Slider:')
slider_label.grid(column=0, row=0, sticky='w')
# slider
slider = ttk.Scale(root, from_=0, to=100, orient='horizontal', command=slider_changed, variable=current_value)
slider.grid(column=1, row=0, sticky='we')
# current value label
current_value_label = ttk.Label(root, text='Current Value:')
current_value_label.grid(row=1, columnspan=2, sticky='n', ipadx=10, ipady=10)
# value label
value_label = ttk.Label(root,text=get_current_value())
value_label.grid(row=2, columnspan=2, sticky='n')
root.mainloop()
Link to site from which I got the answer:- https://www.pythontutorial.net/tkinter/tkinter-slider/
You may try removing this from tkinter ttk import * if you have imported something like this at the top of your code. Simply importing this from tkinter import * helps in showing the values of the scale in slider.
Happy Coding!
A simple like this:
import tkinter as tk
from tkinter import ttk
from tkinter import *
root = tk.Tk()
root.geometry('150x75')
root.resizable(width=False, height=False)
root.eval('tk::PlaceWindow . center')
def sel():
selection = f"Value = str{var.get()}"
label.config(text = selection)
var = tk.DoubleVar()
scale = tk.Scale(root, variable=var )
scale.pack(anchor=CENTER)
label = Label(root)
label.pack()
root.mainloop()
Related
So till now I want to make a simple button but it gives me an error screen, what am I doing wrong? Here's my code:
import tkinter as tk
import math
import time
tk = tk.Tk()
tk.geometry()
tk.attributes("-fullscreen", True)
exit_button = tk.Button(tk, text = "Exit", height = 2, width = 2, command = tk.destroy)
exit_button.place(x=1506, y=0)
tk.mainloop()
You are shadowing tk with something else:
import tkinter as tk
root = tk.Tk()
root.geometry()
root.attributes("-fullscreen", True)
exit_button = tk.Button(root, text="Exit", height=2, width=2, command=root.destroy)
exit_button.place(x=1506, y=0)
tk.mainloop()
You cannot use tk = tk.Tk(), because you are also referring to tkinter as tk. So either:
Change your imports(not recommended):
import tkinter as _tk
tk = _tk.Tk() # And so on..
or change your variable name(recommended):
root = tk.Tk() # And change tk.geometry to root.geometry() and so on
Below is my example code:
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("400x300")
style=ttk.Style()
style.configure("TNotebook", highlightbackground="#848a98") # if I use another option like - background="#848a98" - the style changes, but with - highlightbackground="#848a98" - option the style doesn't change..
MainNotebook = ttk.Notebook(root, style="TNotebook")
MainNotebook.place(x=16, y=16)
Frame1=Frame(MainNotebook, background="#ffffff", width=200, height=150)
Frame1.pack()
Frame2=Frame(MainNotebook, background="#ffffff", width=200, height=150)
Frame2.pack()
MainNotebook.add(Frame1, text="Tab1")
MainNotebook.add(Frame2, text="Tab2")
root.mainloop()
my goal is change the default border color in "#848a98" but the option highlightbackground="#848a98" doesn't work. am I using the wrong instruction? how can I solve my issue?
I think this should do the work. So, as I read it, you need to change the color of the background for focused and not-focused apps; also, for no borders, set the highlight thickness to 0 (ZERO):
from tkinter import *
root = Tk()
e = Entry(highlightthickness=2)
e.config(highlightbackground = "red", highlightcolor= "red")
e.pack()
root.mainloop()
My code:
import tkinter as tk
root = tk.Tk()
for i in range(50):
for j in range(50):
tk.Button(height=1, width=2, bg='Blue').grid(row=j, column=i)
root.mainloop()
I can not see all of the buttons in the screen even when I maxmize the window. so I want to add an option to zoom out (all of the widgets will be smaller) so I can see all of them. How do I do that?
Example code:
import tkinter as tk
root = tk.Tk()
root.state('zoomed')
widgets_to_zoom_list = []
DEFAULT_SIZE = 50
def zoom(widget):
for every_widget in widgets_to_zoom_list:
every_widget.config(width=widget.get(), height=widget.get())
def main():
canvas = tk.Canvas(root)
frame = tk.Frame(canvas)
zoom_scale = tk.Scale(root, orient='vertical', from_=1, to=100)
zoom_scale.config(command=lambda args: zoom(zoom_scale))
zoom_scale.set(DEFAULT_SIZE)
pixel = tk.PhotoImage(width=1, height=1)
for i in range(50):
btn = tk.Button(frame, text=str(i + 1), bg='Blue', image=pixel, width=DEFAULT_SIZE, height=DEFAULT_SIZE, compound="c")
btn.grid(row=0, column=i)
widgets_to_zoom_list.append(btn)
canvas.create_window(0, 0, anchor='nw', window=frame)
# make sure everything is displayed before configuring the scroll region
canvas.update_idletasks()
canvas.configure(scrollregion=canvas.bbox('all'))
canvas.pack(fill='both', side='left', expand=True)
zoom_scale.pack(fill='y', side='right')
root.mainloop()
if __name__ == '__main__':
main()
From what i have been able to understand from your question, i think you want the window to be resizable in tkinter.
To allow a window to be resized by the user, we use the resizable function -:
root.resizable(height = True, width = True)
In this case the two args are height and width which help you customize if you only want the widget to be vertically resizable or only want it to be horizontally resizable.
Your code with this function added should look like this -:
import tkinter as tk
root = tk.Tk()
root.resizable(True, True) #Assuming you want both vertically and horizontally resizable window.
for i in range(50):
for j in range(50):
tk.Button(height=1, width=2, bg='Blue').grid(row=j, column=i)
root.mainloop()
I hope this will solve your problem.
And I also hope you are safe in this time of an ongoing pandemic.
I am trying to set colors to rows in a tkinter treeview object, using tags and tag_configure.
There has been an earlier discussion on coloring rows which is rather old and seems to work no longer for Python3:
ttk treeview: alternate row colors
I have added a brief example. For me, all rows stay white, independent of whether I execute tag_configure prior or after the insert command.
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
w = tk.Label(root, text="Hello, world!")
w.pack()
lb= ttk.Treeview(root, columns=['number', 'text'], show="headings", height =20)
lb.tag_configure('gr', background='green')
lb.column("number", anchor="center", width=10)
lb.insert('',tk.END, values = ["1","testtext1"], tags=('gr',))
lb.insert('',tk.END, values = ["2","testtext2"])
lb.pack()
root.mainloop()
What has changed or what am I missing?
EDIT:
Seems that this is a new known bug with a workaround, but I don't get this working:
https://core.tcl-lang.org/tk/tktview?name=509cafafae
EDIT2:
I am now using tk Version 8.6.10 (Build hfa6e2cd_0, Channel conda-forge) and python 3.7.3. Can anyone reproduce this error with this version of python and tk?
You no longer need to use fixed_map the bug was fixed in tkinter version 8.6.
The following code works fine for me using tkinter 8.6 and python 3.8.2 running in Linux.
import tkinter as tk
import tkinter.ttk as ttk
def fixed_map(option):
return [elm for elm in style.map("Treeview", query_opt=option) if elm[:2] != ("!disabled", "!selected")]
root = tk.Tk()
style = ttk.Style()
style.map("Treeview", foreground=fixed_map("foreground"), background=fixed_map("background"))
w = tk.Label(root, text="Hello, world!")
w.pack()
lb= ttk.Treeview(root, columns=['number', 'text'], show="headings", height =20)
lb.tag_configure('odd', background='green')
lb.tag_configure('even', background='lightgreen')
lb.column("number", anchor="center", width=10)
lb.insert('', tk.END, values = ["1","testtext1"], tags=('odd',))
lb.insert('', tk.END, values = ["2","testtext2"], tags=('even',))
lb.insert('', tk.END, values = ["3","testtext3"], tags=('odd',))
lb.insert('', tk.END, values = ["4","testtext4"], tags=('even',))
lb.pack()
root.mainloop()
That answer of Chuck666 did the trick:
https://stackoverflow.com/a/60949800/4352930
This code works
import tkinter as tk
import tkinter.ttk as ttk
def fixed_map(option):
# Returns the style map for 'option' with any styles starting with
# ("!disabled", "!selected", ...) filtered out
# style.map() returns an empty list for missing options, so this should
# be future-safe
return [elm for elm in style.map("Treeview", query_opt=option)
if elm[:2] != ("!disabled", "!selected")]
root = tk.Tk()
style = ttk.Style()
style.map("Treeview",
foreground=fixed_map("foreground"),
background=fixed_map("background"))
w = tk.Label(root, text="Hello, world!")
w.pack()
lb= ttk.Treeview(root, columns=['number', 'text'], show="headings", height =20)
lb.tag_configure('gr', background='green')
lb.column("number", anchor="center", width=10)
lb.insert('',tk.END, values = ["1","testtext1"], tags=('gr',))
lb.insert('',tk.END, values = ["2","testtext2"])
lb.pack()
root.mainloop()
I hope that Chuck666 copies his answer here since I think he has earned the bonus if he shows up.
I wrote the following Tkinter code. When a user slides the scale widget, the font-size on the label is changed accordingly. I managed to do this but didn't understand why the function i wrote is returning a value when i didn't define it to do so. Is Tkinter returning something invisible implicitly ?
here is the code ...
from tkinter import *
from tkinter import ttk
master = Tk()
master.geometry('650x350+50+200')
scale_1 = ttk.Scale(master, length=300, from_=10, to=60)
scale_1.pack(padx=20, pady=30, anchor='nw')
label_1 = ttk.Label(master, text='Hello World !!!', background='orange')
label_1.pack(padx=10, pady=10, expand=True)
def changeFontSize():
label_1.config(font=('candara', int(scale_1.get())))
scale_1.config(command=changeFontSize) # problem area
master.mainloop()
following was my workaround
def changeFontSize(x)
or
scale_1.config(command= lambda x: changeFontSize())