krassefragen
krassefragen

Reputation: 33

Can't write into tkinter spinbox after filedialog was opened

When I open a filedialog and then write other things into root, I CAN change the spinbox value with the up and down arrows, but I can't click and write into it. What am I doing wrong here? Is this a known bug?

When I don't call filedialog.askopenfilename and enter the Path manually it works fine..

EDIT:

When I click on the drop-down arrow of the combo-box once, I can write into it?!

from tkinter import filedialog, messagebox, Spinbox, Label, Frame, StringVar, Tk, Button, ttk

colors = ["magma", "inferno", "YlOrBr", "YlOrRd", "Wistia"]

# Pfad einlesen

def readPath():
    sheetPath = filedialog.askopenfilename(title = "Choose File",filetypes = (("all files","*.*"),(".xls","*.xls"),(".xlsx","*.xlsx")))
    # sheetPath = "C:/Users/leoch/trends.xls"

class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master

        sensorData = readPath()

        lb = Label(root, text="Settings") #, padx=20, pady=20
        lb.pack()
        root.title("Settings")
        textStart = Label(root, text="From Row: ")
        textStart.pack()
        tvOffset = StringVar(root)
        tvOffset.set=(18)
        spinStart = Spinbox(root, from_=18, to=20, width=7, textvariable=tvOffset)
        spinStart.pack()
        labelFarben = Label(root, text="Colors")
        labelFarben.pack()
        comboFarben = ttk.Combobox(root, values=colors) 
        comboFarben.current(0)
        comboFarben.pack()

        def confirm():
            root.withdraw()
            # visualization(sensorData, rowStart, rowCount, fps, interpolation, farben)

        confirm = Button(root, text="Bestätigen", command=confirm, font=20, padx=20, pady=10, border=2, fg="green")
        confirm.pack(padx=20, pady=20)

root = Tk()
app = Window(root)
root.wm_title("Wäremstromvisualisierung")
root.mainloop()

Upvotes: 3

Views: 140

Answers (1)

user15801675
user15801675

Reputation:

You should add focus_force() after you call readPath()

Also, in order to make the ttk.Combobox read-only, use state='readonly'

from tkinter import filedialog, messagebox, Spinbox, Label, Frame, StringVar, Tk, Button, ttk

colors = ["magma", "inferno", "YlOrBr", "YlOrRd", "Wistia"]

# Pfad einlesen

def readPath():
    sheetPath = filedialog.askopenfilename(title = "Choose File",filetypes = (("all files","*.*"),(".xls","*.xls"),(".xlsx","*.xlsx")))
    # sheetPath = "C:/Users/leoch/trends.xls"

class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master

        sensorData = readPath()
        self.master.focus_force()
        lb = Label(root, text="Settings") #, padx=20, pady=20
        lb.pack()
        root.title("Settings")
        textStart = Label(root, text="From Row: ")
        textStart.pack()
        tvOffset = StringVar(root)
        tvOffset.set=(18)
        spinStart = Spinbox(root, from_=18, to=20, width=7, textvariable=tvOffset)
        spinStart.pack()
        labelFarben = Label(root, text="Colors")
        labelFarben.pack()
        comboFarben = ttk.Combobox(root, values=colors,state='readonly') 
        comboFarben.current(0)
        comboFarben.pack()

        def confirm():
            root.withdraw()
            # visualization(sensorData, rowStart, rowCount, fps, interpolation, farben)

        confirm_b = Button(root, text="Bestätigen", command=confirm, font=20, padx=20, pady=10, border=2, fg="green")
        confirm_b.pack(padx=20, pady=20)

root = Tk()
app = Window(root)
root.wm_title("Wäremstromvisualisierung")
root.mainloop()

Upvotes: 2

Related Questions