Python Forum
Adding text after temperature reading Tkinter
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding text after temperature reading Tkinter
#1
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.
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,730 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Updating tkinter text BliepMonster 5 5,970 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,858 Jun-26-2022, 06:26 PM
Last Post: menator01
  tkinter change the text of the checkbox zazas321 1 3,830 Sep-17-2021, 06:19 AM
Last Post: zazas321
  tkinter text widget word wrap position chrisdb 6 7,567 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,639 Mar-10-2021, 04:21 PM
Last Post: Sir
Photo Tkinter TEXT background image _ShevaKadu 5 7,767 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter | Button color text on Click Maryan 2 3,366 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Text Upload LearningLittlebyLittle 0 2,047 Sep-04-2020, 07:55 PM
Last Post: LearningLittlebyLittle
  Adding an image to a tkinter window djwilson0495 2 8,079 Aug-23-2020, 11:07 AM
Last Post: ebolisa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020