0

I have an exercise with tkinter library. I write code with gui in PyCharm but if I run a code nothing happen.

I tried to run that in cmd prompt but script is running again in PyCharm and nothing happen.

I tried also run script with cmd prompt as default to open .py format but nothing happen again.

Do you have any suggestions how can I run my script and see GUI ?

Here is my code:

import tkinter, sys

def koniec():
    sys.exit()

def zmiana():
    l.config(text = 'Wcisnij zakoncz')

main = tkinter.Tk()

l = tkinter.Label(main, text = 'Wcisnij przycisk ponizej')
b = tkinter.Label(main, text = 'Zakoncz', command = koniec())
b2 = tkinter.Label(main, text = 'Przycisk', command = zmiana)

l.pack()
b.pack()
b2.pack()


main.mainloop()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
lenncb
  • 35
  • 5
  • replace `b = tkinter.Label(main, text = 'Zakoncz', command = koniec()) ` with `b = tkinter.Label(main, text = 'Zakoncz', command = koniec)` without the calling paren `()`. – Reblochon Masque Jun 29 '19 at 13:56

1 Answers1

1

here's what i came up with, i included a way to center the gui in your monitor maybe it will come in handy yes? anyway good luck on your project

import sys
from tkinter import Label, Tk, TRUE, FALSE, Button

window = Tk()
window.resizable(width=TRUE, height=FALSE)
window.title("Try this :)")
window.geometry("250x150")
def center(window):
    window.update_idletasks()
    w = window.winfo_screenwidth()
    h = window.winfo_screenheight()
    size = tuple(int(_) for _ in window.geometry().split('+')[0].split('x'))
    x = w/2 - size[0]/2
    y = h/2 - size[1]/2
    window.geometry("%dx%d+%d+%d" % (size + (x, y)))
center(window)

def koniec():
    window.destroy()


def zmiana():
    l.config(text = 'Wcisnij zakoncz')

#---------------------------------------------------------------------------------#

l = Label(window, text="Wcisnij przycisk ponizej:")
b = Button(text="Close?", command=koniec )
b2 = Button(text = 'Przycisk', command = zmiana)

l.pack()
b.pack()
b2.pack()
#Main Starter
window.mainloop()
  • also i used button instead of label, you're running a command my friend, command is like do something, label well it's just for labeling, y'know what i mean right? so if you need to execute commands from your definitions you best use a button or something that would trigger an event like mouse or keyboard or yes a button – Louie Jay Orellana Jayme Jun 29 '19 at 14:35
  • @lenncb no problem bro :) up-vote for my answer if its useful thanks – Louie Jay Orellana Jayme Jun 29 '19 at 15:38