why tkinter button is not visible while running the code? - python

button is not visible while executing the program
def frame1():
# label(text displayed on the frame1)
label1 = Label(root,text="Welcome to DeciTree!\n\nThis is a desktop GUI app written in Python \nthat builds a predictive model \nusing a Decision tree algorithm called ID3\n\nClick NEXT to continue ",
anchor=CENTER, height=14, width=70, background='light green')
label1.config(font=('comic sans', 38, 'bold'))
label1.pack()
# button(next button on frame1)
button1 = Button(root, text="NEXT", height=30,width=30, command=frame2)
button1.config(font=('comic sans',20,'bold'))
button1.pack(side=RIGHT)

I can't see all of the code but have you closed it off with root.mainloop()? and if so, does it print the labels? Do you get a crash report? Maybe you have to root.update() your program to display the button? Do you have a big enough area to display the button? Does frame2 work properly?

Related

Python | (Tkinter) only shows text when button is pressed

So I made a button to copy something to clipboard, but the button itself is always showing, but the text on it only when its pressed, how to fix it?
(Here the part with the button code:)
canvas1 = tk.Canvas(root, width=300, height=300)
canvas1.pack()
def copy_button():
clip = tk.Tk()
clip.withdraw()
clip.clipboard_clear()
clip.clipboard_append(pw)
clip.destroy()
button1 = tk.Button(text="Copy to Clipboard", command=copy_button, bg="grey", fg="white", font=("Helvetica", 12, "bold"))
canvas1.create_window(150, 150, window=button1)
Your disappearing text issue does not appear in Linux OS but I suspect your issue is related to your button1 = tk.Button(....) statement.
The first argument to a tk.Button widget must be its parent (see this link on this requirement) and not a keyword/option.
Try button1 = tk.Button(canvas1, text="Copy to Clipboard", ....). Doing so, you define the tk.Button to be a child of the tk.Canvas widget.
If you are putting more than a button in the canvas, you might want to consider:
defining a tk.Frame to be a child of the tk.Canvas(e.g. frame1=tk.Frame(canvas1)),
replace window=button1 with window=frame1,
letting button1 to be a child of frame1, e.g. button1 = tk.Button(frame1, text="Copy to Clipboard", ....)
Do let me know if this answer addresses your issue.
Other suggestion:
You can remove clip = tk.Tk(), clip.withdraw(), clip.destroy() and replace the clip term in clip.clipboard_clear() and clip.clipboard_append(pw) with root. Here, I assume that you had earlier defined root = tk.Tk() at the start of your code.

Trying to get my GUI windows automatically exit when I open the next window

How can I manage to automatic cancel this 1st window when I click the next window button?
sample code:
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("GUI practice")
def open():
top = Toplevel() # new window
top.title("Kokomi")
labels = Label(top, text="This one automatically close when i click the next window").pack()
button2 = Button(top,text="Close window", command=top.destroy).pack()
button3 = Button(top,text="Next window", command=open2).pack()
def open2():
top = Toplevel() # new window
top.title("Guide")
labels = Label(top, text="end").pack()
button2 = Button(top, text="Close window", command=top.destroy).pack() # destroy to quit things
button = Button(root, text="Open(No need to close this)", command=open).pack()
root.mainloop()
[Click open][1]
[Click Next window and after that this windows should disappear and continue to the 3rd picture][2]
[The 2nd picture goes disappear when i click the next window][3]
[1]: https://i.stack.imgur.com/plS1T.png
[2]: https://i.stack.imgur.com/EFW76.png
[3]: https://i.stack.imgur.com/xSZCp.png
For this specific case of using two functions and two windows, you can just simply rename the Toplevel widgets to different names and then globalize one and then access it from another function and destroy it before the new windows is shown.
def open():
global top
top = Toplevel() # new window
top.title("Kokomi")
Label(top, text="This one automatically close when i click the next window").pack()
Button(top, text="Close window", command=top.destroy).pack()
Button(top, text="Next window", command=open2).pack()
def open2():
top.destroy() # Destroy previously open window
top1 = Toplevel() # new window
top1.title("Guide")
Label(top1, text="end").pack()
Button(top1, text="Close window", command=top1.destroy).pack() # destroy to quit things
If you noticed, I removed the variable names of buttons and labels because its useless to have those as their value is None, read Tkinter: AttributeError: NoneType object has no attribute <attribute name>.
When you wish to use more functions and windows, you have to manually follow this procedure for all the functions. Unless ofcourse there is a better and cleaner way of designing your app using classes and frames.
Alternatively you can also invoke two functions from a single button, this method will get rid of globalization and renaming and might be a bit better than the above mentioned solution:
def open():
top = Toplevel() # new window
top.title("Kokomi")
Label(top, text="This one automatically close when i click the next window").pack()
Button(top, text="Close window", command=top.destroy).pack()
Button(top, text="Next window", command=lambda: [top.destroy(),open2()]).pack()
def open2():
top = Toplevel() # new window
top.title("Guide")
Label(top, text="end").pack()
Button(top, text="Close window", command=top.destroy).pack() # destroy to quit things

Thread in Tkinter starts automatically [duplicate]

This question already has an answer here:
Tkinter Button command getting executed before clicking the button [duplicate]
(1 answer)
Closed 2 years ago.
I am trying to add a thread to tkinter button to trigger when its presses. but as soon as I run the program, the function inside the button starts automatically before I click anything.
Here is my code:
m = Main()
root = Tk()
root.geometry("500x500")
frame = Frame(root)
frame.pack()
start = Button(frame, text="Start",
command=threading.Thread(target=m.main).start())
stop = Button(frame, text="stop", command=quit)
start.pack(pady=20)
stop.pack()
root.mainloop()
The reason why the function runs without you clicking the button is because you are adding parentheses after the function.
So you would need to change this line:
start = Button(frame, text="Start",
command=threading.Thread(target=m.main).start())
to this:
start = Button(frame, text="Start",
command=threading.Thread(target=m.main).start)
All you have to do is take out the parentheses, which tells python that you only run the function if the button is clicked. If you include the parentheses, python takes it as a normal function call and ignores the button completely.
Full code: as per what you gave in your question
m = Main()
root = Tk()
root.geometry("500x500")
frame = Frame(root)
frame.pack()
start = Button(frame, text="Start",
command=threading.Thread(target=m.main).start)
stop = Button(frame, text="stop", command=quit)
start.pack(pady=20)
stop.pack()
root.mainloop()
or you could use lambda.

How do you make scrolling pages in a tkinter gui with python 3, like on a website?

I am making a small program with the tkinter plugin in python. I want to make a page scrolling as you can do in the browser. Is it possible? and how do you do it?
window2 = Tk()
window2.title("RPG SM Beta 0.1")
window2.geometry('150x200')
window2.configure(background="#5C5858")
window2.iconbitmap("Logo.ico")
window2.resizable(False, False)
write1 = Text(window2, height=5, width=17, bg="white")
write1.place(x=6, y=30)
lab2 = Label(window2, text='Lorem ipsum', bg="#5C5858", fg='white', font='none 12 bold')
lab2.place(x=4, y=5)
The most command solution is to use a Canvas widget as the container. Anything added to a canvas with the create_window method will be scrollable.

Destroy window not closing all windows properly

I have two queries in this section.
In my code i have created two frames under root, the first frame have "NEXT" button to go on second frame. In second frame there is Run button, which has mapped with close_window function. It should close all windows properly. But in my case not closing it.
When i click "Run" i need to close all windows and need to execute another script on the same directory. Is that possible to do it ?
from Tkinter import *
def close_window():
frame2.destroy()
frame1.destroy()
def swap_frame(frame):
frame.tkraise()
root = Tk()
root.geometry("900x650+220+20")
root.title("Testing")
root.configure(borderwidth="1", relief="sunken", cursor="arrow", background="#dbd8d7", highlightcolor="black")
root.resizable(width=False, height=False)
frame2 = Frame(root, width=900, height=650)
frame1 = Frame(root, width=900, height=650)
Button1 = Button(frame1, text="Next", width=10, height=2, bg="#dbd8d7", command=lambda: swap_frame(frame2))
Button1.place(x=580, y=580)
Button2 = Button(frame2, text="Run", width=10, height=2, bg="#dbd8d7", command=close_window,)
Button2.place(x=580, y=580)
frame2.grid(row=0, column=0)
frame1.grid(row=0, column=0)
root.mainloop()
what is wrong in the code?
"Destroy window not closing all windows properly"
Nor should it. destroy method destroys a widget, and when a widget is destroyed so are its children.
Since neither frame1 nor frame2 are 'windows' or a window's parents, there's no window destruction taking place.
"When I click "Run" I need to close all windows and need to execute another script in the same directory. Is that possible to do it?"
It is possible. Use quit on any GUI object, instead of destroy. It stops the mainloop, hence destroying the entire GUI. In that it solves the first problem as well. Then import another_script:
...
def close_window():
frame1.quit()
...
root.mainloop()
import another_script

Categories