feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

tkinter root.title

Sat Jan 09, 2016 4:00 pm

Importing Tkinter as Tk with python3 , and following a .py script line root=Tk() with root.title=("xxxxx") the gui displays Tk as the box title on my raspi but displays xxxxx when running the same script on my windows PC. What do I need to correct to make it work on the raspi, please?
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: tkinter root.title

Sat Jan 09, 2016 6:12 pm

Try running the app and then stretching the window sideways. Tkinter seems to not expand the window to make the title visible.

Alternatively add the line
root.geometry('300x100')

feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

Re: tkinter root.title

Sat Jan 09, 2016 10:57 pm

RogerW wrote:Try running the app and then stretching the window sideways. Tkinter seems to not expand the window to make the title visible.

Alternatively add the line
root.geometry('300x100')
Thanks for the interest. I'd managed to suss that, and also the x and y offsets, but have now fallen foul to another raspi/PC anomaly. Raspi does not like fg='red' or bg='yellow' to set foreground and background colours, when changing font properties, though the PC does.
Doing a sudo apt-get install python-tk on the raspi reports my raspi installed version is the 'latest', whatever that may mean in real life. :-(
This all seems against the tutorials I have seen on the internet, which advise use of fg and bg but I am beginning to suspect the reference material may be outdated and no longer apply. Does anyone please have pointer(s) to the latest definitive tutorial for tkinter, if possible beginning with .py script 'import' code that is known to work on raspi and Python3 and the 'latest' tkinter and goes on to set up a change of font colour?
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: tkinter root.title

Sun Jan 10, 2016 9:38 am

Are you sure you are using python3? If you start idle and select help/about this will show which versions of python and tkinter you are using. I ask this because you quote Tkinter not tkinter which is the python 3 version. It may be relavent which OS you have installed - raspian Jessie has python 3.4.2 which is fairly recent.

It would help to see some context. This snippet works to give red text.

Code: Select all

        # create text display
        self.textid = self.create_text(self.centrex
        ,self.centrey - 3*self.blobrad
        ,fill = 'red'
        ,font = tkf.Font(size = -int(2*self.majortick)))
in this case self is a class derived from tkinter.canvas. tkf is tkinter.font

feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

Re: tkinter root.title

Sun Jan 10, 2016 2:19 pm

Somewhat isolated from the niceties of civilisation, in Central America, I am trying to use some old and newer Raspis I brought with me from Europe, in this particular case to use DS18B20's to measure temperature and to monitor the results remotely from the workshop over a LAN. That anything works is more down to the excellent guidance given me in these forums and what I can glean on the internet. I began, knowing zilch about Python. I have never knowingly used Python2.

In terminal, I am running the .py script by invoking :-

Code: Select all

 
cd scripts
python3 vapour.py 
#scripts is the directory I created where I stored the .py files
Those actions evidently scan vapour.py and, if this throws up problems, returns to the terminal display with indications of the position in the .py script that confuses it and a description of the nature of the problem it sees.
From the internet, I copied the invocation of Tkinter at the top of the .py script:-

Code: Select all

 
from tkinter import Tk, Label
import time


root = Tk()
root.title('dalVapour')
root.geometry("800x450+50+50")
stit=Label(root, text='Heat ON at 12:17')
stit.config(font=('Arial',20))
timing=Label(root, text='At 15:57')
timing.config(font=('Arial',20))
fist=Label(root,text='temperature now is')
fist.config(font= ('Arial', 20))
sayit = Label(root, text='96.3' + '  degrees Fahrenheit')
sayit.config(font=('times', 48, 'italic bold'), fg='red', bg='yellow')
# Font is a tuple of (font_family, size_in_points, style_modifier_string)
stit.pack(ipady=20)
timing.pack(ipady=20)
fist.pack(ipady=20)
sayit.pack(pady=20,ipadx=20,ipady=20)
root.after(15000,root.destroy)
root.mainloop()
This 'works' on my PC but when I try it modified as an added part to the working temperature-measuring front end, on my Raspi, using mainly the good Adafruit tutporial data, it does not.
You have kindly given me useful extra pointers I shall go away and try. If it works, I shall offer it on the forums as a way of reporting temperature results using tkinter.
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: tkinter root.title

Sun Jan 10, 2016 2:42 pm

I have just tried your code on my pi and it works the same as on my PC.

I am using Rasbian Jessie, python 3.4.2 tkinter 8.6.2

Incidentally if you are using DS18B20 sensors you might find this useful.

Code: Select all

# ds18b20.py
# written by Roger Woollett

import os
import glob
import time

class DS18B20:
	# much of this code is lifted from Adafruit web site
	# This class can be used to access one or more DS18B20 temperature sensors
	# It uses OS supplied drivers and one wire support must be enabled
	# To do this add the line
	# dtoverlay=w1-gpio
	# to the end of /boot/config.txt
	#
	# The DS18B20 has three pins, looking at the flat side with the pins pointing
	# down pin 1 is on the left
	# connect pin 1 to GPIO ground
	# connect pin 2 to GPIO 4 *and* GPIO 3.3V via a 4k8 (4800 ohm) pullup resistor
	# connect pin 3 to GPIO 3.3V
	# You can connect more than one sensor to the same set of pins
	# Only one pullup resistor is required
	
	def __init__(self):
		# load required kernel modules
		os.system('modprobe w1-gpio')
		os.system('modprobe w1-therm')
		
		# Find file names for the sensor(s)
		base_dir = '/sys/bus/w1/devices/'
		device_folder = glob.glob(base_dir + '28*')
		self._num_devices = len(device_folder)
		self._device_file = list()
		i = 0
		while i < self._num_devices:
			self._device_file.append(device_folder[i] + '/w1_slave')
			i += 1
		
	def _read_temp(self,index):
		# Issue one read to one sensor
		# you should not call this directly
		f = open(self._device_file[index],'r')
		lines = f.readlines()
		f.close()
		return lines
		
	def tempC(self,index = 0):
		# call this to get the temperature in degrees C
		# detected by a sensor
		lines = self._read_temp(index)
		retries = 5
		while (lines[0].strip()[-3:] != 'YES') and (retries > 0):
			# read failed so try again
			time.sleep(0.1)
			#print('Read Failed', retries)
			lines = self._read_temp(index)
			retries -= 1
			
		if retries == 0:
			return 998
			
		equals_pos = lines[1].find('t=')
		if equals_pos != -1:
			temp = lines[1][equals_pos + 2:]			
			return float(temp)/1000
		else:
			# error
			return 999
			
	def device_count(self):
		# call this to see how many sensors have been detected
		return self._num_devices
This is a test program

Code: Select all

# Temp.py

from ds18b20 import DS18B20	
	
# test temperature sensors
x = DS18B20()
count=x.device_count()
i = 0
while i < count:
	print(x.tempC(i))
	i += 1
Put the two files in the same directory and run the second.

feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

Re: tkinter root.title

Sun Jan 10, 2016 5:44 pm

Thanks to you and all the other help received, I have finished and tested the prototype. I have returned home and am now sitting in my bedroom able to RDP the Raspi in the workshops, from my PC, so am controlling that to show me the remote workshop temperature readings. I'll go on to develop it further as I find time. I don't want to bore folks with the full untidied-up code but if anyone is interested, I'll gladly post it here.
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

Return to “Python”