Python Forum
[Tkinter] Tkinter won't start
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter won't start
#1
Hello everyone!
I faced with difficult that when I run this code:

from tkinter import Tk, Canvas

root = Tk()
c = Canvas(root, width=800, height=800, bg='black')
c.pack()
c.create_text(100, 50, anchor='w', fill='orange', font='Arial 28 bold underline', text='....')
   

...it gives me: Process finished with exit code 0, but Tkinter's window doesn't appear.
Reply
#2
It started and then ended quite abruptly. Add root.mainloop() to the end of the program.
uriel likes this post
Reply
#3
When creating a GUI with tkinter you need to have a single mainloop that is called at the point you want your GUI to run.
It creates an infinite loop that runs until the main tkinter window is destroyed.
The mainloop will draw the GUI widgets and react to events such as button presses, as you have seen without it the GUI doesn't function.
from tkinter import Tk, Canvas
 
root = Tk()
c = Canvas(root, width=800, height=800, bg='black')
c.pack()
c.create_text(100, 50, anchor='w', fill='orange', font='Arial 28 bold underline', text='....')
root.mainloop()
Reply
#4
Thanks a lot! I am learning from a book and there is no mention of mainloop on it. Huh
Reply
#5
Also i want to ask about other issue: Did i make a mistake in the code with reading the contents of the file or again with dispaying the lines of the file?

from tkinter import Tk, Canvas
from datetime import date, datetime


def get_events():
    list_events = []
    with open('events.txt', encoding='utf-8') as file:
        for line in file:
            line = line.rstrip('\n')
            current_event = line.split(',')
            event_date = datetime.strptime(current_event[1], '%d/%m/%Y').date()
            current_event[1] = event_date
            list_events.append(current_event)
    return list_events


def days_between_dates(date1, date2):
    time_between = str(date1 - date2)
    number_of_days = time_between.split(' ')
    return number_of_days[0]


root = Tk()
c = Canvas(root, width=800, height=800, bg='black')
c.pack()
c.create_text(100, 50, anchor='w', fill='orange', font='Arial 28 bold underline', text='Календарь ожидания')
root.mainloop()

events = get_events()
today = date.today()

vertical_space = 100

for event in events:
    event_name = event[0]
    days_until = days_between_dates(event[1], today)
    display = '%s через %s дн.' % (event_name, days_until)
    c.create_text(100, 100, anchor='w', fill='light blue', font='Arial 28 bold', text=display)

vertical_space = vertical_space + 30
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter function to clear old canvas and start a fresh "operation" zazas321 5 9,637 Oct-01-2020, 04:16 AM
Last Post: zazas321

Forum Jump:

User Panel Messages

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