Python Forum
GUI skips code part - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: GUI skips code part (/thread-32665.html)



GUI skips code part - MLGpotato - Feb-24-2021

Hello!
Can anyone help me? I dont know why but it just litearly skips gamemain(). :/ Wall Currently there are no errors in the code.
from tkinter import *
import random
import tkinter
import time

def gamemain(): #It skips this code
    gametext0 = Label(root , text = "1")
    gametext1 = Label(root , text ="2")
    gametext2 = Label(root, text ="3")
    gametext3 = Label(root , text = "4")
    gameinput0 = Entry(root , width=50, borderwidth=5)
    enter = Button(root, text = "Name your self", command = main())
    gametext0.pack()
    gametext1.pack()
    gametext2.pack()
    gametext3.pack()
    gameinput0.pack()
    enter.pack()
    root.mainloop()
def main():#and goes straight to this
    root.destroy()
    window = Toplevel()
    time.sleep(8)
    gametext0 = Label(window , text = "5")
    gametext1 = Label(window , text = "6")
    gametext2 = Label(window , text = "7")
    gametext3 = Label(window , text = "8")
    gametext4 = Label(window , text = "9")
    gametext5 = Label(window , text = "10")
    gametext0.pack()
    gametext1.pack()
    gametext2.pack()
    gametext3.pack()
    gametext4.pack()
    gametext5.pack()
    window.mainloop()



def start1():
    global root

    root = Toplevel()
    def securitycheck():
        if secCheck.get() == "123":
            print("Lets go!")
            command = gamemain()
        else:
            print("Nope. Invalid.")
    secCheckt = Label(root, text="Please show me your lisence mister:")
    secCheck = Entry(root, width=50, borderwidth=5)
    secCHK = Button(root, text = "Check", command = securitycheck)
    secCheckt.pack()
    secCheck.pack()
    secCHK.pack()
    secCheck.insert(0, "Code")
    root.mainloop()
def close():
    exit()



print("Game")
global root
root = Tk()
root.geometry = "100X100"
buttonA = Button(root, text="START", command=start1)
buttonB = Button(root, text="CLOSE", command=close)
buttonA.pack()
buttonB.pack()
root.mainloop()
Any help Confused


RE: GUI skips code part - menator01 - Feb-24-2021

# This should be
enter = Button(root, text = "Name your self", command = main())

#this
enter = Button(root, text = "Name your self", command = main)



RE: GUI skips code part - deanhystad - Feb-24-2021

It does not skip gamemain(), main() is being called from inside gamemain() as mentioned by menator01. Execution never returns to gamemain() after making the button, so you don't pack any of the labels, or the button. Since you don't pack the labels and button they do not become visible.

There should only be 1 mainloop(). It should be very near the bottom of your main module and it should be called using the window returned by Tk(). The call to mainloop() never returns unless you quit the GUI.

You are still thinking you are writing a console program. A GUI program has to be structured completely differently and really requires a different way of thinking about the problem. I suggest you do a few tkinter tutorials before trying to write your own program.


RE: GUI skips code part - MLGpotato - Feb-25-2021

(Feb-24-2021, 07:46 PM)deanhystad Wrote: It does not skip gamemain(), main() is being called from inside gamemain() as mentioned by menator01. Execution never returns to gamemain() after making the button, so you don't pack any of the labels, or the button. Since you don't pack the labels and button they do not become visible.

There should only be 1 mainloop(). It should be very near the bottom of your main module and it should be called using the window returned by Tk(). The call to mainloop() never returns unless you quit the GUI.

You are still thinking you are writing a console program. A GUI program has to be structured completely differently and really requires a different way of thinking about the problem. I suggest you do a few tkinter tutorials before trying to write your own program.
I have done my mini projekt with console app. I thought I would just step up a bit. But I think you know more. Anyways, thanks for the reply. Smile


RE: GUI skips code part - buran - Feb-27-2021

(Feb-25-2021, 05:59 AM)MLGpotato Wrote: I have done my mini projekt with console app. I thought I would just step up a bit
Actually separating the GUI part and the logic is good approach. For this your console script has to be properly structured. Then you can easily use the same code as part of GUI application. We haven't seen your console script, but given the fact that you use globals here I doubt it's properly structured though.


RE: GUI skips code part - deanhystad - Feb-28-2021

I don't know that I agree that it is "easy" to port a console program to be a GUI program. A console program has has a fixed sequence. The operator enters information when prompted by the program. You don't have to worry about step A occurring before step B because the steps are ordered by the program.

In a GUI program the sequence is dictated by the user. If step A has to happen before step B, you must take steps that the operator has performed step A before you make step B available. But if you strictly regulate what the user can do at any given time it detracts from the power of a good user interface. When I am writing a Word document I don't have to select a font and set the line spacing and margins before I can begin typing. The application lets me perform these operations at any time and applies them to the document. You should strive for a similar experience in your applications. There should be your data, and the operations you can perform on your data. And for the most part the order of operations should be flexible.

I find GUI applications easier to write than console programs, probably because I'm used to writing event driven programs and that way of thinking has become more natural to me. But I very vaguely remember struggling with with the shift in paradigm when graphical interfaces were first coming into being. Of course the tools available for use back then were pretty horrible.


RE: GUI skips code part - buran - Feb-28-2021

(Feb-28-2021, 05:36 AM)deanhystad Wrote: I don't know that I agree that it is "easy" to port a console program to be a GUI program.
you missed the prerequisite for "easy"
(Feb-27-2021, 11:07 AM)buran Wrote: For this your console script has to be properly structured.

With the same well thought backend you can have console interface and GUI


RE: GUI skips code part - deanhystad - Feb-28-2021

I think it is easy to take the logic out of a GUI application and use it to make a console application, but a console application can skip a lot of the logic required by a GUI application just because it has linear execution. Even if you "properly structure" your console app into nice functions and data structures you are not even half way along on converting to a GUI.