The function opens by itself in Tkinter [duplicate] - python

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
I have a problem, I want to make a fontions which opens a site when I press the button except that as soon as I launch the file the functions are executed by themselves
from tkinter import *
import random
import string
import webbrowser
def Tokens():
webbrowser.open_new("https://xlean.me")
button = "Start"
windows = Tk()
windows.title("Discord Tokens")
windows.geometry("500x150")
windows.minsize(500,150)
windows.maxsize(500,150)
windows.iconbitmap("icon.ico")
windows.config(background="#242424")
text = Label(windows, text="Hello to you !", bg="#242424", fg='white')
text.config(font=("Alatsi", 30))
text.pack()
button = Button(windows, text=button, bg='#202020', fg='white', command=Tokens())
button.pack(fill=X)
windows.mainloop()

You should pass the Tokens function as the command arg, while you are passing the result of its execution instead. You need to remove the parenthesis after command=Tokens. Here is the fixed code:
from tkinter import *
import random
import string
import webbrowser
def Tokens():
webbrowser.open_new("https://xlean.me")
button = "Start"
windows = Tk()
windows.title("Discord Tokens")
windows.geometry("500x150")
windows.minsize(500,150)
windows.maxsize(500,150)
windows.iconbitmap("icon.ico")
windows.config(background="#242424")
text = Label(windows, text="Hello to you !", bg="#242424", fg='white')
text.config(font=("Alatsi", 30))
text.pack()
button = Button(windows, text=button, bg='#202020', fg='white', command=Tokens)
button.pack(fill=X)
windows.mainloop()

Related

how to save texts that has been written in an entry in python [duplicate]

This question already has answers here:
How do I get the Entry's value in tkinter?
(2 answers)
Closed 17 days ago.
enter image description here
when i write in the execution's entry i want this to be saved in another text file
how ?
and this is my code
from tkinter import ttk
from tkinter import *
root = Tk()
label1=ttk.Label(root,text="Type your message : ",font="classic")
label1.pack()
entry1=ttk.Entry(root,width=70)
entry1.pack()
button=ttk.Button(root,text="Send",padding=7,cursor="hand2")
button.pack()
def Bclick () :
entry1.delete(0,END)
print("sent")
button.config(command=Bclick)
file = input("yo :")
root.mainloop()
with open('text','w') as myfile :
myfile.write(file)
To get the value from an Entry widget, you need to use its get method, e.g.,
from tkinter import ttk
from tkinter import *
root = Tk()
label1 = ttk.Label(root, text="Type your message : ", font="classic")
label1.pack()
entry1 = ttk.Entry(root, width=70)
entry1.pack()
button = ttk.Button(root, text="Send", padding=7, cursor="hand2")
button.pack()
def Bclick():
entryvalue = entry1.get() # get what's in the Entry widget before clearing it
entry1.delete(0,END)
print("sent")
# write the value to a file
with open("text", "w") as myfile:
myfile.write(entryvalue) # write it to your file
button.config(command=Bclick)
#file = input("yo :") # not sure what this was for, so I've commented it out
root.mainloop()
After this, your file text should contain whatever you entered into the Entry box.
What your current code is doing is building the interface then it collects input through the terminal and writes it to a file, and then finally it displays the window to the user.
What you want to do instead is inside of your buttons callback method, collect the input from the Entry widget, and save whatever it contains in the your buttons callback.
from tkinter import ttk
from tkinter import *
filename = "MyTextFile.txt"
root = Tk()
label1=ttk.Label(root,text="Type your message : ",font="classic")
label1.pack()
entry1=ttk.Entry(root,width=70)
entry1.pack()
button=ttk.Button(root,text="Send",padding=7,cursor="hand2")
button.pack()
def Bclick():
text = entry1.get()
with open(filename, "wt") as fd:
fd.write(text)
entry1.delete(0,END)
print("sent")
button.config(command=Bclick)
root.mainloop()

Why won't text in tkinter change? [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 1 year ago.
I am having a trouble changing text in python even though though that I saw a lot of use it
My code:
from tkinter import *
window = Tk()
def change_text():
btn.config(text = "second text")
btn = Button(window ,text= "first text" , command= change_text).pack()
window.mainloop()
You assigned the return value of .pack() to btn, and pack doesn't return the widget it was called on (it returns None implicitly, since it has no useful return value). Just split up the creation of the button from the packing:
from tkinter import *
window = Tk()
def change_text():
btn.config(text="second text")
btn = Button(window, text="first text", command=change_text)
btn.pack()
window.mainloop()
your code is good but it is not good to make .pack() in the same line
from tkinter import *
window = Tk()
def change_text():
btn.config(text = "second text")
btn = Button(window ,text= "first text" , command= change_text )
btn.pack()
window.mainloop()
it works just will

Can someone help me with parameters in tkinter "command" [duplicate]

This question already has an answer here:
Tkinter assign button command in a for loop with lambda [duplicate]
(1 answer)
Closed 1 year ago.
I'm trying to make a little file explorer with just buttons, I'm still in the early stages and want to make a function prints which button was pressed and came up with this:
import tkinter as tk
buttons = []
window = tk.Tk()
window.geometry("200x100")
def open(button):
print(button)
def list(titles):
i=0
while i<(len(titles)):
btn = tk.Button(text=titles[i], width=20, command=lambda: open(i))
buttons.append(btn)
buttons[i].grid(row=i, column=1)
print(f"adding {titles[i]}")
i=i+1
list(["title1", "title2", "title3"])
window.mainloop()
There's one problem: It always prints 3. I think I know what the problem is, i always stays 3 after generating the button so it passes 3 to the function, but I don't know how to solve it.
I used the lambda cuz I cant pass parameters just using open(i) and found the lambda-solution to that on this question .
Can someone help?
Tnx!
Because it over writes the commands on one button when you assign it again. Do:
import tkinter as tk
buttons = []
window = tk.Tk()
window.geometry("200x100")
def open(button):
print(button)
def list(titles):
btn = tk.Button(text=titles[0], width=20, command=lambda: open(1))
buttons.append(btn)
buttons[0].grid(row=1, column=1)
print(f"adding {titles[0]}")
btn = tk.Button(text=titles[1], width=20, command=lambda: open(2))
buttons.append(btn)
buttons[1].grid(row=2, column=1)
print(f"adding {titles[1]}")
btn = tk.Button(text=titles[2], width=20, command=lambda: open(2))
buttons.append(btn)
buttons[2].grid(row=3, column=1)
print(f"adding {titles[2]}")
list(["title1", "title2", "title3"])
window.mainloop()

How to change a line of text in a tkinter window every few seconds in python [duplicate]

This question already has answers here:
How do you run your own code alongside Tkinter's event loop?
(5 answers)
Closed 3 years ago.
I am trying to display a random phrase from a dictionary every few seconds in a tkinter window.
I can get the phrase to display by just running a variable into a text box in tkinter, but I can't seem to get that phrase to change in the desired intervals.
So far this is the code that I have.
import time
import sys
import random
import tkinter as tk
from tkinter import *
""" DICTIONARY PHRASES """
phrases = ["Phrase1", "Phrase2", "Phrase3"]
def phraserefresh():
while True:
phrase_print = random.choice(phrases)
time.sleep(1)
return phrase_print
phrase = phraserefresh()
# Root is the name of the Tkinter Window. This is important to remember.
root=tk.Tk()
# Sets background color to black
root.configure(bg="black")
# Removes the window bar at the top creating a truely fullscreen
root.wm_attributes('-fullscreen','true')
tk.Button(root, text="Quit", bg="black", fg="black", command=lambda root=root:quit(root)).pack()
e = Label(root, text=phrase, fg="white", bg="black", font=("helvetica", 28))
e.pack()
root.mainloop()
The result of running this code is that the tkinter window never opens, as opposed to changing the displayed text. I know I must be over looking something simple but I can't seem to figure out what. Thanks for you help in advance!
This function never returns due to the while True loop:
def phraserefresh():
while True:
phrase_print = random.choice(phrases)
time.sleep(1)
return phrase_print # This line is never reached
You can use the after() method to set up a repeating delay and change the label text.
def phrase_refresh():
new_phrase = random.choice(phrases)
e.configure(text=new_phrase) # e is your label
root.after(1000, phrase_refresh) # Delay measured in milliseconds

Tkinter check button not working when in another window [duplicate]

This question already has an answer here:
How do I create child windows with Python tkinter?
(1 answer)
Closed 6 years ago.
When you run this code and try and check the the check button and click the button to print out the value of the check button it does not work. I can't figure out why. It prints out 0 whether checked or unchecked.
from tkinter import *
def awesome():
def click_me():
print(var.get())
return
root = Tk()
root.title("a good try")
var = IntVar()
x = Checkbutton(root, text = "check me", variable = var)
y = Button(root, text = "click me", command = click_me)
x.pack()
y.pack()
root.mainloop()
return
def main():
main = Tk()
cool = Button(main, text = "click", command = awesome)
cool.pack()
main.mainloop()
main()
change root = Tk() to root = Toplevel()
need to use Toplevel() for a window that opens on another window.

Categories

Resources