Python Forum
how to center askstring tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to center askstring tkinter
#2
Quote:i want the "password = askstring('Password', 'Please entet your password:', show="*") to show up in the center of the screen.
Are you sure about this? The common practice is to have dialogs appear where the user is looking, at the cursor. In your example the dialog should appear over the button you press that causes the dialog to appear. Normally you would create your toplevel window and set the master to the master (root) window. When toplevel window is drawn, it appears near the master window.

But if you really want to have a window appear at a specific location you can set the location along with the size in the geometry() command.

You cannot call Tk() multiple times in one program. Tk() initializes tkinter and creates a toplevel window. Call Tk() only once. Call Toplevel() to create additional toplevel windows.

You are using global wrong. Use global inside a function that wants to assign a variable defined in the global space. In your code you define "win" as global, but you never use "win" outside the password space, so it should not be defined as global.

You should not be using geomerty() to set the size of a window or place() to set the location of a button. Learn how to use pack() and grid() to create windows that size themselves to fit their contents. This makes it easy to change things like button and label text, fonts, even images without having to update all the place() commands. Using layout managers like pack() and grid() also let you make resizable windows.
While learning python you may as well learn the common coding conventions. Following coding conventions in your code makes it easier for others to read your code. Being familiar with the conventions will make it easier for you to read code written by others.

Don't use wildcard imports (import tkinter as *). Wildcard imports are bad. It is easy to accidentally overwrite variables or functions defined in your file with ones defined in a module imported this way. And even worse, there is no warning when this happens. I know a lot of tkinter examples on the internet use from tkinter import *, there is even a tkinter package (tkinter.ttk) that is designed to take advantage of the silent overwriting, but wildcard imports are at best confusing, and have potential for creating bugs that are very difficult to find.

Use docstrings to describe what functions do. Avoid using inline comments right of a python statement.Instead of this:
    def fileExplorer():
      new_win = Tk()  # Create a new window instance for File Explorer
Do this:
def fileExplorer():
    # Create a new window instance for File Explorer.
    new_win = Toplevel()
Or better yet do this.
def fileExplorer():
    """Create a new window instance for File Explorer."""
    new_win = Toplevel()
Function and variable names should be snake_case. The Password() function should be the password() function. fileExplorer should be file_explorer.

Blank lines should be used to help identify code blocks. Two blank lines before and after a global function, one blank line before and after an enclosed function.

Speaking of enclosed functions, why do you have so many? Enclosed functions should be uncommon. Can those functions be defined globally? I have a difficult time reading your code because you mix your function definitions inline with the body of your code. Place functions at the top of the file, or enclosed functions at the top of the enclosing function. The function body should not be split up by function definitions or imports.
Reply


Messages In This Thread
how to center askstring tkinter - by ruddlev - Aug-01-2024, 03:16 PM
RE: how to center askstring tkinter - by deanhystad - Aug-01-2024, 05:15 PM
RE: how to center askstring tkinter - by ruddlev - Aug-01-2024, 05:26 PM
RE: how to center askstring tkinter - by ruddlev - Aug-01-2024, 05:30 PM
RE: how to center askstring tkinter - by deanhystad - Aug-01-2024, 06:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  (OpenCV) Help to improve code for object detection and finding center of it saoko 0 2,192 May-14-2022, 05:34 PM
Last Post: saoko
  [S0LVED] [Tkinter] Center dialog? Winfried 8 8,046 Mar-01-2022, 07:17 PM
Last Post: Winfried
  Program to move a dot towards a circle center plumberpy 10 7,389 Dec-03-2021, 12:20 PM
Last Post: BashBedlam
  Center align Kristenl2784 1 2,754 Aug-03-2020, 04:25 PM
Last Post: bowlofred
  Image : center and resizing vvv 1 13,504 May-14-2017, 03:41 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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