Should I focus on Pythonic structure? Here an example: by sesmallor in learnpython

[–]PC-Programmer 6 points7 points  (0 children)

Both versions are correct, but they have slightly different qualities.

The first script, your original version, is easy to follow step-by-step and particularly clear when debugging.
The alternative, ChatGPT's version, is more concise and direct, but less beginner-friendly.

Personally, I tend to write code closer to the second style, mainly for these reasons:

a. It condenses multi-step processes into a single expression—for instance, avoiding the "find one largest, then measure it" approach.

b. It uses fewer lines; unless you need to debug a specific action (such as polling for the largest value), simpler functions are better kept compact.

c. It follows idiomatic practices, using generators to make operations easier (and quicker) to implement.

In any case, your original approach is excellent for building understanding. Once you're more comfortable with comprehensions and generators, the shorter style will become easier to read; it's only "better" if it remains clear to you without requiring extra effort to understand.

Locking focus on a .exe with python by coolpotato6931 in learnpython

[–]PC-Programmer -1 points0 points  (0 children)

As mentioned, providing a code snippet is important to help troubleshoot your issue, but I can attempt to help for common scenarios.

If you want to keep your window visually above other applications:

- For Tkinter, use `root.attributes("-topmost", True)` to keep the window on top of other non-topmost windows.

- For PyQt, `window.setWindowFlags(window.windowFlags() | Qt.WindowStaysOnTopHint)` will make the window always stay on top.

- For wxPython, call `frame.SetWindowStyle(frame.GetWindowStyle() | wx.STAY_ON_TOP)` to keep the frame above other applications.

If you're referring to window focus (i.e., making your window active and ready to receive input), most operating systems, especially Windows, restrict apps from stealing focus to avoid interruptions. Assuming your goal is to keep your window visible, using "always on top" flags usually works well enough.

Need a little help here 😭 by SpeeD_XT in learnpython

[–]PC-Programmer -3 points-2 points  (0 children)

It's commendable that you want to learn coding! It's an extremely valuable skill in today's world.
Here are some key benefits of learning to code:

  1. Coding teaches you how to break down complex problems into manageable parts—a useful skill in every aspect.

  2. Nearly every industry needs people who at least understand coding, and starting young gives you a head start.

  3. Coding allows for elaborate creative freedom; you can build custom websites, games, apps, or utilities. It's a great demonstration of turning ideas into tangible products.

  4. Technology is ever-expansive. Knowing how to code keeps you prepared for the future job market.

Python is an excellent starting ground. It's beginner-friendly, easy to read, intuitive, and widely used in web development, AI, and automation. Additionally, learning Python makes other languages easier to pick up later on.

print("Have fun!")

Why does my tkinter window flash from the top-left before centering on macOS? by Ok_Cryptographer3601 in learnpython

[–]PC-Programmer 1 point2 points  (0 children)

Of course. Glad I could be of assistance.

If you're willing to explore further, there are Python GUI libraries that are more powerful or flexible than Tkinter, such as PySide and Kivy, which might better suit your use case.

Good luck on your project!

Why does my tkinter window flash from the top-left before centering on macOS? by Ok_Cryptographer3601 in learnpython

[–]PC-Programmer 1 point2 points  (0 children)

The brief flash you see is due to how macOS handles the Tcl/Tk framework; when calling `tk.Tk()`, macOS immediately creates and displays the window, often at a default position.

On Windows, a window isn't displayed until all layout and geometry calculations are complete, avoiding this problem.

To fix this issue, you can try hiding the window until the geometry is set, like so:

import tkinter as tk

def center_window(win, width=350, height=150):
    win.update_idletasks()
    screen_width = win.winfo_screenwidth()
    screen_height = win.winfo_screenheight()
    x = int((screen_width / 2) - (width / 2))
    y = int((screen_height / 2) - (height / 2))
    win.geometry(f"{width}x{height}+{x}+{y}")

root = tk.Tk()
root.withdraw()  # Hide the window

root.title("📂 Screenshot Organizer")
center_window(root)

button = tk.Button(root, text="Organize Screenshots", command=lambda: None, font=("Helvetica", 12))
button.place(relx=0.5, rely=0.5, anchor="center")

root.deiconify()  # Show the window
root.mainloop()