简体   繁体   中英

Python Tkinter entry-point

So for a project on my school I am trying to make an GUI where I can add some info for some email so it will send it automatically through an python script.

Now I do have a small problem. I made some entry-points but for some reason I can not add text to the left of the entr-ypoints. Does anyone know how to do that?

I am trying to get 'sending to:', 'name:', and 'course:' to the left of the corresponding entry-points.

It might sounds silly, but please do not change to much of the actual code. It has to be like this for the project.

Kind regards,

Allard


import os
import smtplib
import tkinter as tk

root =tk.Tk()

root.title("Title")

def main():

    USER = os.environ.get('USERNAME_GMAIL')
    PASSWORD = os.environ.get('PASSWORD_GMAIL')
    print("-"*60)
    print("\nNew Email\n\n")


    with smtplib.SMTP('smtp.gmail.com',587) as smtp:

        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()
        smtp.login(USER, PASSWORD)



        PROFESSOR = entry2.get() 
        COURSE = entry3.get()

        subject = 'Course literature: ' + COURSE
        body = 'Body of the email, doesnt really matter whats in here' + PROFESSOR

        msg= f'Subject:{subject}\n\n{body} '

        smtp.sendmail(USER, RECEIVER, msg)


canvas1 = tk.Canvas(root, height=400, width= 400)
canvas1.pack()

entry1=tk.Entry(root)
canvas1.create_window(200, 60, window=entry1)

entry2=tk.Entry(root)
canvas1.create_window(200, 80, window=entry2)

entry3=tk.Entry(root)
canvas1.create_window(200, 100, window=entry3)



def PRINT():
    x1 = entry1.get()
    x2 = entry2.get()
    x3 = entry3.get()
    label1= tk.Label(root, text = 'Sending to: ' + str(x1))
    canvas1.create_window(200,200, window=label1)

    label2= tk.Label(root, text = 'Name: '+ str(x2))
    canvas1.create_window(200,220, window=label2)

    label3= tk.Label(root, text = 'Course: '  +str(x3))
    canvas1.create_window(200,240, window=label3)


button1 = tk.Button(text = 'TEST', bg="black", fg="white", font=('helvetica', 9, 'bold'), command=PRINT)
canvas1.create_window(200,130, window=button1)


button2 = tk.Button(text = 'Verzenden via email', bg = "red", fg= "white", font=('helvetica', 9, 'bold'), command=main)
canvas1.create_window(200,160, window=button2)

You should not be trying to place widgets at exact coordinates. Tkinter has managers for handling layout in a logical manner: pack and grid . These take care of problems with different window sizes, different resolutions, and different fonts, and are superior to placing elements at specific x/y coordinates.

Since you want to create a grid of label/entry pairs, grid is the correct choice.

entry1=tk.Entry(root)
entry2=tk.Entry(root)
entry3=tk.Entry(root)
label1 = tk.Label(root, text="Sending to:")
label2 = tk.Label(root, text="Name:")
label3 = tk.Label(root, text="Course:")

label1.grid(row=0, column=0)
entry1.grid(row=0, column=1)
label2.grid(row=1, column=0)
entry2.grid(row=1, column=1)
label3.grid(row=2 column=0)
entry3.grid(row=2, column=1)

You need to save the item ID of each canvas1.create_window(...) and then use the item ID to find the position of the entry and put a text before the entry using canvas1.create_text(...) :

...
# function to put a prompt before the 'entry' item
def create_prompt(prompt, entry):
    # get the bounding box of the Entry item
    bbox = canvas1.bbox(entry)
    # create a canvas text item and put it before the Entry item like below:
    #  (x-5,y)   (x,y)
    # -------+   +-----------------+
    #  prompt:   | Entry           |
    #            +-----------------+
    #
    # note that bbox[0] is the x-coordinate of the top-left of entry item
    #           bbox[1] is the y-coordinate of the top-left of entry item
    # using anchor='ne' means that the (x, y) of the text is the top-right corner of the text
    # and finally return the item ID of the canvas text item
    return canvas1.create_text(bbox[0]-5, bbox[1], text=prompt, anchor='ne')

entry1=tk.Entry(root)
entry1_item = canvas1.create_window(200, 60, window=entry1)
create_prompt('sending to:', entry1_item)

entry2=tk.Entry(root)
entry2_item = canvas1.create_window(200, 80, window=entry2)
create_prompt('name:', entry2_item)

entry3=tk.Entry(root)
entry3_item = canvas1.create_window(200, 100, window=entry3)
create_prompt('course:', entry3_item)
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM