Home > Blockchain >  Tkinter StringVar
Tkinter StringVar

Time:10-27

Hi folks I'm trying to add a function that goes up by 0.2 everytime i click on the button. I was able to do it with a IntVar (1, 2, 3 ...) but now I'm just to dumb for that. It should do both, the step by 1, 2, 3 etc and the 0.2, 0.4, 0.6 etc.

from tkinter import *
from tkinter.ttk import *


root = tkinter.Tk()
root.geometry("200x200")
root.minsize("300", "200")
root.maxsize("400", "300")
root.title("Test")


counter = tkinter.IntVar()
fish = tkinter.StringVar()
fished = 0


def onClick(event=None):
    fish.set(str(fished   0.2))
    counter.set(counter.get()   1)
    print(counter.get())
    num1 = counter.get() * 0.2
    print(num1)


messagevar = Message(root, textvariable = fish)
messagevar.config(bg="lightgreen")
messagevar.pack()

tkinter.Label(root, textvariable=counter).pack()
tkinter.Button(root, text="increase", command=onClick, fg="black", bg = "white").pack()



root.mainloop()```

CodePudding user response:

You can increase the string counter the same way as you do with the integer counter:

import tkinter


def on_click():
    fish.set(f'{float(fish.get())   0.2:.1f}')
    counter.set(counter.get()   1)


root = tkinter.Tk()
root.geometry("200x200")

counter = tkinter.IntVar()
fish = tkinter.StringVar(value='0')

message_var = tkinter.Message(root, textvariable=fish, bg="lightgreen")
message_var.pack()

tkinter.Label(root, textvariable=counter).pack()
tkinter.Button(root, text="increase", command=on_click, fg="black", bg="white").pack()

root.mainloop()

Just get the value, convert it to a float, add the .2, also format it because of the float precision.

Also you can configure the background when initializing the object and your given code wouldn't actually run because name tkinter isn't defined.

Additionally:
I strongly advise against using wildcard (*) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.

I strongly suggest following PEP 8 - Style Guide for Python Code. Function and variable names should be in snake_case, class names in CapitalCase. Don't have space around = if it is used as a part of keyword argument (func(arg='value')) but have space around = if it is used for assigning a value (variable = 'some value'). Have space around operators ( -/ etc.: value = x y(except here value = x y)). Have two blank lines around function and class declarations.

CodePudding user response:

Integers don't store decimal places by definition; you have to use a float type. You could use DoubleVar() instead of IntVar(). However, you could just use a variable as you are attempting to do.

from tkinter import *
from tkinter.ttk import *


root = tkinter.Tk()
root.geometry("200x200")
root.minsize("300", "200")
root.maxsize("400", "300")
root.title("Test")


counter = tkinter.DoubleVar()
fish = tkinter.StringVar()
fished = 0


def onClick(event=None):
    global fished
    fished  = 0.2  # equivalent to fished = fished   0.2
    fish.set(str(fished))
    counter.set(counter.get()   0.2)
    print(counter.get())


messagevar = Message(root, textvariable = fish)
messagevar.config(bg="lightgreen")
messagevar.pack()

tkinter.Label(root, textvariable=counter).pack()
tkinter.Button(root, text="increase", command=onClick, fg="black", bg = "white").pack()



root.mainloop()
  • Related