Python Forum

Full Version: Adding text after temperature reading Tkinter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all, i haven't messed with python for a couple of years, so im very rusty. Im messing with a gui i started making for another project, now i need it for something else. Im pretty sure the way i did this with self.root makes the window as big as the text, so i just need to add text after the temperature reading, that way the temperature reading says "Inside Temp" after it prints the temp. Here is the code im working with;

import Tkinter as tk
master = tk


def temp():
   tfile = open("/sys/bus/w1/devices/28-000006c51ea2/w1_slave")
   text = tfile.read()
   tfile.close()
   secondline = text.split("\n")[1]
   temperaturedata = secondline.split(" ")[9]
   temperature = float(temperaturedata[2:])
   temperature = temperature / 1000 * 9.0 / 5.0 +32
   temperature = round(temperature, 2)
   return temperature

class App():

   def __init__ (self) :
       self.root = tk.Tk()
       self.root.title("auto")
       self.label = tk.Label(text="")
       self.label.pack()
       self.update_clock()
       self.root.mainloop()
       
   def update_clock(self):
       self.label.configure(text=temp())
       self.root.after(3000, self.update_clock)

app = App()
Any help is appreciated. Thank you.
I tried a little bit. It's confusing :-D
You can use tkfont to update the Fontsize.
To get a resize Event, you should use root.bind('<Configure>', callback).
The event has also a type-code. I've seen type code '22' (it's a str) when resizing the window.
As callback I'm using lamda to supply the callback also with the font object.
Inside the callback function resize, I get the min value of height and width and convert it to a negative value, which means changing the size in pixel and not as dots.

import Tkinter as tk
import tkFont
import random

master = tk

def temp():
  return random.randint(-400, 400)

def resize(event, font):
   if event.type == '22':
       font.configure(size=-min(event.height, event.width))

class App():

  def __init__ (self) :
      self.root = tk.Tk()
      self.root.title("auto")
      self.root.geometry("600x200")
      self.font = tkFont.Font(self.root, size=30)
      self.root.bind('<Configure>', lambda x: resize(x, self.font))
      self.label = tk.Label(text="", font=self.font)
      self.label.pack()           
      self.update_clock()
      self.root.mainloop()

  def update_clock(self):
      self.label.configure(text=temp())
      self.root.after(3000, self.update_clock)

app = App()
The example is not perfect. Sometimes I see a strange behavior.
Not sure if I misunderstand this but isn't it just:

   def update_clock(self):
      self.label.configure(text=temp() + " Inside Temp")
      self.root.after(3000, self.update_clock)
Or do you need to do something like:
  def update_clock(self):
     mytemp = temp() + " Inside Temp"
      self.label.configure(text= mytemp)
      self.root.after(3000, self.update_clock)