简体   繁体   中英

how do i finish my tkinter in my program?

So I made a program which finds a factor of a number my problem is how do i print the name into the tkinter i asked first the name of the user then ask what number and show the factor of that number then the program asks the user if he wants to enter another one the 0 means to exit i wanted it once he types 0 to show a thank you in tkinter with his name "Thank you! name for using my program!"

I'm fairly new to tkinter and i'm a pretty bad learner i dont know how to write his name in tkinter

from tkinter import *

print("Finding Factors Program by Joshua Lozada")

name=input("what is your name?")
print ("Hello",name, " Welcome To My Finding factors Program")


def factor_finder(x): 
    print("The factors of",x,"are:")
    for i in range(1, x + 1):     
        if x % i == 0:
            print(i) 


while True:
    try:
        num = int(input("Enter an integer (0 to exit): "))
        if num == 0:
             window = Tk()
             window.title("Thank you!")
             window.configure(background="black")
             Label (window, bg="black") 
             Label(window, text = "Thank you for using my program!" , bg 
="black", fg="white", font= "none 12 bold") .grid(row=0, column=0, 
sticky=W)
             break
        factor_finder(num)
    except ValueError:
        print("Sorry, you must enter an integer")

i want the program once ended show tkinter and show a thank you name for using my program!

You should put your tkinter code outside of the game loop; that is upon termination, the mainloop should exit ( break ), then execute the tkinter code:

Maybe like this?

import tkinter as tk

print("Finding Factors Program by Joshua Lozada")

name=input("what is your name?")
print ("Hello",name, " Welcome To My Finding factors Program")


def factor_finder(x): 
    print("The factors of",x,"are:")
    for i in range(1, x + 1):     
        if x % i == 0:
            print(i) 

while True:
    try:
        num = int(input("Enter an integer (0 to exit): "))
        if num == 0:
            break
        factor_finder(num)
    except ValueError:
        print("Sorry, you must enter an integer")

window = tk.Tk()
window.title("Thank you!")
window.configure(background="black")
tk.Label(window, bg="black") 
tk.Label(window, text="Thank you for using my program!", bg="black", fg="white", font= "none 12 bold").grid(row=0, column=0, sticky=W)

window.mainloop()

here's what you need to do in order to print name along with the message on the tkinter window

Label(window, text = "Thank you "+name+" for using my program" , bg 
="black", fg="white", font= "none 12 bold") .grid(row=0, column=0, 
sticky=W)

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