Python Forum
how to center askstring tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to center askstring tkinter
#1
Hello, im fairly new to Tkinter and I want to know how can i center the following askstring for a password to the window.

heres my code so far:
from tkinter import *
from tkinter.messagebox import showinfo
from tkinter.simpledialog import askstring
from tkinter import simpledialog
top = Tk()
top.title("Lockscreen")
top.geometry("1175x1000")
top.config(bg="dark khaki")
def Password():
  global win  # Declare win as global
  win = Tk()
  win.geometry("1175x1000")
  win.config(bg="dark khaki")
  password = askstring('Password', 'Please enter your password:', show="*")
i want the "password = askstring('Password', 'Please entet your password:', show="*") to show up in the center of the screen.


here is my whole code incase I need to change it for the password:
from tkinter import *
from tkinter.messagebox import showinfo
from tkinter.simpledialog import askstring
from tkinter import simpledialog
top = Tk()
top.title("Lockscreen")
top.geometry("1175x1000")
top.config(bg="dark khaki")
def Password():
  global win  # Declare win as global
  win = Tk()
  win.geometry("1175x1000")
  win.config(bg="dark khaki")
  password = askstring('Password', 'Please enter your password:', show="*")
  if password == ("Password"):
    win.title("Desktop")  # Assign title to the existing win instance
    win.geometry("1175x1000")
    win.config(bg="dark khaki")
    def searchBar():
      search = askstring('Search Bar', 'Search the web')
      showinfo('Error', 'URL not found!')
      command=searchBrowser()
    def searchBrowser():
      new_win = Tk()  # Create a new window instance for the browser
      new_win.title("Web Browser")
      new_win.geometry("1175x1000")
      new_win.config(bg="dark khaki")
      B = Button(new_win, text="Search Bar", command=searchBar)
      B.place(x=450, y=25)
      new_win.mainloop() # Start the event loop for the browser window
    B = Button(win, text="Web Browser", command=searchBrowser)
    B.place(x=25, y=25)
    def Images():
      new_win = Tk()  # Create a new window instance for Images
      new_win.title("Images")
      new_win.geometry("536x262")
      new_win.mainloop() # Start the event loop for the Images window
    def Minesweeper():
      import subprocess
      subprocess.run(["python", "Minesweeper.py"])
    def downloads():
      new_win = Tk()  # Create a new window instance for Downloads
      new_win.title("Downloads")
      new_win.geometry("1175x1000")
      new_win.config(bg="dark khaki")
      B = Button(new_win, text="Minesweeper.exe", command=Minesweeper)
      B.place(x=25, y=25)
      new_win.mainloop() # Start the event loop for the Downloads window
    def fileExplorer():
      new_win = Tk()  # Create a new window instance for File Explorer
      new_win.title("File Explorer")
      new_win.geometry("1175x1000")
      new_win.config(bg="dark khaki")
      B = Button(new_win, text="Downloads", command=downloads)
      B.place(x=25, y=25)
      B = Button(new_win, text="Images", command=Images)
      B.place(x=25, y=55)
      new_win.mainloop() # Start the event loop for the File Explorer window
    B = Button(win, text="File Explorer", command=fileExplorer)
    B.place(x=25, y=55)
    win.mainloop()  # Start the event loop for the Desktop window
  if password != ("Password"):
    showinfo(title="Error", message="Password Incorrect")
B = Button(top, text ="Login", command = Password)
B.place(x=480,y=250)
top.mainloop()
deanhystad write Aug-01-2024, 03:22 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#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
#3
[quote="deanhystad" pid='180207' dateline='1722532559']

Quote: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.


I dont really know exactly what i am doing. I just put the codes in as i went along.
Reply
#4
(Aug-01-2024, 05:15 PM)deanhystad Wrote:
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.

(Aug-01-2024, 05:26 PM)ruddlev Wrote: [quote="deanhystad" pid='180207' dateline='1722532559']

Quote: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.


I dont really know exactly what i am doing. I just put the codes in as i went along.

should I just rewrite the whole thing in its entirety or just edit it?
Reply
#5
I would start over and start small. Get the lock screen and password to work first. Once you are happy with that, show the window with the options for different things to do, but maybe with just one option. Once you get that working, look at how the option window works with the lock screen. Should these be separate windows, or one window that changes the view from a lock screen to the option window?
Reply


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,051 May-14-2022, 05:34 PM
Last Post: saoko
  [S0LVED] [Tkinter] Center dialog? Winfried 8 7,484 Mar-01-2022, 07:17 PM
Last Post: Winfried
  Program to move a dot towards a circle center plumberpy 10 6,954 Dec-03-2021, 12:20 PM
Last Post: BashBedlam
  Center align Kristenl2784 1 2,643 Aug-03-2020, 04:25 PM
Last Post: bowlofred
  Image : center and resizing vvv 1 13,301 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