Python Forum
[Tkinter] Text input OK on Windows, not working on linux
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Text input OK on Windows, not working on linux
#1
I have created a Tkinter form with the help of Figma/Tkinter.Designer. On Windows (Python 3.12) it works Ok. When I tried it on Linux Armbian/Bookworm (Python 3.11), I encountered an issue with the form's input field: when I clicked on it, it did not get focus, so I could not input anything on it. There are no errors reported at all.
So, to demonstrate the issue I left on the form just an input with its event handler and a button with its handler. I don't know how even to begin troubleshooting ...

import os, sys
#import socket
from pathlib import Path
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage

myPath = os.path.dirname(os.path.abspath(sys.argv[0]))
ASSETS_PATH = myPath + "/assets/frame0"

def relative_to_assets(path: str) -> Path:
    return ASSETS_PATH / Path(path)

maxhours = 12
posx = 1700
posy = 500

entryFont = ("Consolas", 10, "bold") 

def skeyevent(e):
    # allow digits, 2 characters long
    s = seconds.get("1.0", "end-1c")
    count = len(s)
    if e.char.isdigit():
        count += 1
    if count > 2:  # and e.keysym not in {'BackSpace', 'Delete'}:
        seconds.delete("1.0", "end")
        s = s[0:2]
        seconds.insert("end-1c", str(s).rjust(2, "0"))
        return("break")
    if not (
        e.char.isdigit()
        or e.keysym == "Delete"
        or e.keysym == "BackSpace"
    ):
        return "break" 

def okClicked():        
    ss = seconds.get("1.0", "end-1c") 
    if ss=='':
        s=0
    else:
        s = int(ss)
        if s > 59:
            s = 59
    seconds.delete("1.0", "end")
    seconds.insert("end-1c", str(s).rjust(2, '0'))
    print(f"set,{str(s).rjust(2, "0")}")
    #quit()

def exitClicked():
    quit()    

window = Tk()

window.geometry(f"160x123+{posx}+{posy}")
window.overrideredirect(True)
window.attributes("-topmost", True)
window.configure(bg="#12707D")

canvas = Canvas(
    window,
    bg="#12707D",
    height=123,
    width=160,
    bd=0,
    highlightthickness=0,
    relief="ridge",
)
canvas.place(x=0, y=0)
canvas.create_rectangle(1.0, 28.0, 159.0, 122.0, fill="#D9D9D9", outline="")

canvas.create_text(
    4.0,
    100.0,
    anchor="nw",
    text="Seconds",
    fill="#000000",
    font=("SegoeUI", 10 * -1,"bold"),
)

imgSeconds = PhotoImage(file=relative_to_assets("entry.png"))
seconds_bg = canvas.create_image(64.0, 106.0, image=imgSeconds)
seconds = Text(bd=0, bg="#FFFFFF", fg="#000716", highlightthickness=0, font= entryFont)
seconds.place(x=55.0, y=95.0, width=18.0, height=20.0)
seconds.bind("<Key>", skeyevent)

imgOk = PhotoImage(file=relative_to_assets("ok.png"))
buttonOk = Button(
    image=imgOk,
    borderwidth=0,
    highlightthickness=0,
    command=lambda: okClicked(),
    relief="flat",
)
buttonOk.place(x=83.0, y=33.0, width=70.0, height=22.0)

canvas.create_text(
    48.0,
    8.0,
    anchor="nw",
    text="Enter Time",
    fill="#FFFFFF",
    font=("Inter Bold", 12 * -1),
)
window.resizable(False, False)
window.mainloop()
The script expects to find following two pictures in that Assets folder:
https://otalado.eu/Tkinter/entry.png
https://otalado.eu/Tkinter/ok.png
Reply
#2
I'm not sure about the rest as of yet but,
return ASSETS_PATH / Path(path) should be
return f'{ASSETS_PATH} / {Path(path)}'
or the like. You have a forward slash not in quotes.


I do not understand the need for all the return("break") either.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
(Sep-18-2024, 08:40 AM)menator01 Wrote: I'm not sure about the rest as of yet but,
return ASSETS_PATH / Path(path) should be
return f'{ASSETS_PATH} / {Path(path)}'
or the like. You have a forward slash not in quotes.


I do not understand the need for all the return("break") either.

Hi, the two things you suspect are not the issue. I made a small app with just one entry and a button and played with it. It turns out that it works on Windows but not on Linux. When I removed the line:
window.overrideredirect(True)
to re-enable form to allow the title bar on the form, the whole thing started working Ok on Linux too.
Now I need to figure out how to hide the title bar without that "side effect".
Reply
#4
window.overrideredirect(True)
is OK on Windows,
window.wm_attributes('-type', 'splash')
is OK on Linux.

I was unable to find a way to hide the title bar which would work on both without creating some issues. Fortunately, it is easy for Python to figure out on which OS it is running and adapt to it accordingly.
But if there is someone who knows if there is a way common for both OS's, I'd like to know howit
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  popup, text input with two readable buttons ethompson 7 1,923 Aug-28-2024, 03:40 AM
Last Post: deanhystad
  Scaling text QLabel following display mode windows '100%, 125% ...) VIGNEAUD 2 3,143 Jul-07-2021, 06:38 PM
Last Post: deanhystad
  [Tkinter] Text.search() regexp not working rfresh737 11 7,377 Apr-16-2021, 06:56 PM
Last Post: rfresh737
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 15,865 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  Tkinter scaling windows conten to or with its size not working Detzi 5 6,645 Jan-12-2020, 12:42 PM
Last Post: Detzi
  [Tkinter] Paste Operation not working in Text Widget Code_Enthusiast 1 3,848 Sep-11-2019, 08:49 PM
Last Post: Larz60+
  Set input text from button gio123 1 3,532 Mar-01-2018, 10:28 AM
Last Post: gio123
  Please advice Linux library - tray icon, popup windows, ICQ/Skype style etc ramendik 5 5,184 Dec-03-2017, 04:35 AM
Last Post: Larz60+
  popup message box code runs in Windows, but not in Linux Luke_Drillbrain 13 18,619 May-10-2017, 01:05 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

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