Handling cross-platform differences 

Even though Tkinter is a cross-platform module, you might come across occasions when code written for one operating system might not work as expected on other operating systems.  We already saw one such example in the case of getting command-line results in the previous Redirecting the command-line output to Tkinter. In such cases, you can overcome these cross-platform discrepancies by first identifying the operating system on which the program is being run and then using a conditional statement to run different lines of code for different operating systems.

Here's a brief snippet that demonstrates this concept:

 from platform import uname
operating_system = uname()[0]
if ( operating_system == "Linux" ):
canvas.bind('<Button-4>', wheelUp) # X11
canvas.bind('<Button-5>', wheelDown)
elif ( operating_system == "Darwin" ):
canvas.bind('<MouseWheel>', wheel) # MacOS
else:
canvas.bind_all('<MouseWheel>', wheel) # windows

The particular problem here is that the mouse wheel event is denoted by the <MouseWheel> event name on Windows and macOS, but as <Button-4> and <Button-5> on Linux distributions.  The preceding code uses the platform module of Python to identify the operating system and follows a different line of code for different operating systems.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.106.7