Home>

I want to set the maximum value of the slider to the value entered in the text box.


(* I want the value in the box on the left to be the maximum value of the slider)

ValueError
invalid literal for int () with base 10:''


Since the value of self.max is empty from the error statement, I am aware that I get an error saying that the code is invalid for int ().
I'm trying to set the initial value of self.max to 250 with self.start_up (), so I'm at a loss because I don't know what to do.

Corresponding source code
import tkinter as tk
from tkinter import ttk
class Application (tk.Frame):
    def __init __ (self, master): #Initial setting
        super () .__ init __ (master)
        self.pack ()
        self.master.title ("GUI")
        self.slider ()
        self.start_up ()
    def slider (self): #slider setting
        self.frame = tk.Frame (self.master)
        self.frame.pack (side = tk.LEFT)
        self.Label_max = tk.Label (self.frame, text = "renge")
        self.Label_max.pack (side = tk.LEFT)
        self.max = tk.StringVar ()
        self.max_entry = ttk.Entry (self.frame, textvariable = self.max, width = 3)
        self.max_entry.pack (side = tk.LEFT)
        M = self.max.get ()
        self.scale = tk.Scale (self.frame, orient ='horizontal', from_ = 0, to = int (M))
        self.scale.pack (side = tk.LEFT)
    def start_up (self): #Maximum slider initial value
         self.max.set (250)
root = tk.Tk ()
app = Application (master = root)
app.mainloop ()
Supplementary information (FW/tool version, etc.)

windows 64bit
Python 3.7.9
VSCode 1.51.1

  • Answer # 1

    In the slider function, with no string set in variableM = self.max.get ()As
    An error has occurred because we are trying to convert to an int type while keeping the empty string.
    Because the initial value set does not have to be a separate function
    Whether to do self.max.set (250) before doing self.max.get () in the slider function as it is
    Or if you want to divide by function, call the start_up function before doing self.max.get ().

    You can set the event as follows to reflect the numerical value on the slider by inputting the value.

      def slider (self):
            ・
            ・
            self.max_entry.bind ('<KeyRelease>', self.slider_set)
        def slider_set (self, event):
            now = self.max.get ()
            if int (now):
                self.scale ['to'] = now
                self.scale.set (now)

  • Answer # 2

    Function within the initialization functionstart_upFunction beforesliderIs probably due to running. Try changing the order.