I'm new to python, and I'm trying to work with “tkinter”.
The bottom line is that I need to assign a folder deletion function to the button
my code looks like this
from tkinter.ttk import LabeledScale
import shutil
import pathlib
master = tk.Tk()
lable = tk.Label(text ="delete this?")
lable.pack()
path = "C:\\Users\\kolba\\Desktop\\pythonpool"
def buttonClick():
shutil.rmtree(path)
button = tk.Button(master, text ="yes!!!", ) #what to put after the comma?
button.pack()
master.mainloop()
how do I make the button work?
You are looking for command
Function or method to be called when the button is clicked.
In this case
def buttonClick():
shutil.rmtree(path)
button = tk.Button(master, text="yes!!!", command=buttonClick)
You should pass the function after the comma:
button = tk.Button(master, text ="yes!!!", command = buttonClick)
You can read more about tk.Button here
you could add a font and you should put a command otherwise that button will be for nothing. something like this:
from tkinter.ttk import LabeledScale
import shutil
import pathlib
master = tk.Tk()
lable = tk.Label(text ="delete this?")
lable.pack()
path = "C:\\Users\\kolba\\Desktop\\pythonpool"
def buttonClick():
shutil.rmtree(path)
button = tk.Button(master, text ="yes!!!",command=buttonClick )
button.pack()
master.mainloop()
Related
I am trying to develop a GUI with tkinter. I have created a function (as a button) to browse for a directory folder ('input folder'). I have a routine linked to another button ('execute') that needs the path from 'input folder'.
I am getting errors when I try to pass the path from 'input folder' into os.chdir inside 'execute'. Example is as follows:
import sys
import os
from tkinter import filedialog
from tkinter import *
window = Tk()
def Input():
filename = filedialog.askdirectory()
global filename
def Extraction():
in_loc = filename
os.chdir(in_loc)
btn = Button(window, text="Extract", bg="black", fg="white", command=Extraction)
btn.pack()
btn2 = Button(text="Input", command=Input).pack()
window.mainloop()
Can anyone reproduce this and tell me what I am doing wrong here?
Thanks :)
Try this:
import sys
import os
from tkinter import filedialog
from tkinter import *
filename = ''
def input_function():
global filename
filename = filedialog.askdirectory()
def extraction():
global filename
in_loc = filename
os.chdir(in_loc)
window = Tk()
btn = Button(window, text="Extract", bg="black", fg="white", command=extraction)
btn.pack()
btn2 = Button(text="Input", command=input_function).pack()
window.mainloop()
filename p1.py
import tkinter as tk
def action():
import p2
root=tk.Tk()
root.title('part1')
root.geometry('200x200+50+50')
btn=tk.Button(root,text='click me',command=action)
btn.pack()
root.mainloop()
filename p2.py
After closing this window, I want to reopen it on clicking the click me button but it does not open once I close this window.
import tkinter as tk
root=tk.Toplevel()
root.title('part2')
root.geometry('200x200+50+50')
lbl=tk.Label(root,text='Hello everybody \n I have problem',font=("times new roman",20,'bold'))
lbl.pack()
root.mainloop()
Here's a solution for you:
Module_one:
import tkinter as tk
def action():
import action_module
action_module.page_two()
root = tk.Tk()
root.title('part1')
root.geometry('200x200+50+50')
btn = tk.Button(root, text='click me', command=action)
btn.pack()
root.mainloop()
action_module:
def page_two():
import tkinter as tk
root = tk.Toplevel()
root.title('part2')
root.geometry('200x200+50+50')
lbl = tk.Label(root, text='Hello everybody \n I think the problem is fixed',
font=("times new roman", 20, 'bold'))
lbl.pack()
root.mainloop()
Just put the code in the second module inside a function. Then call it inside the first file's action function.
I want to create a tkinter window, where it will appear the files of a folder as a dropdown menu and a Select button, such that when I select an element from the previous list the full path will be saved into a new variable. Apparently, I need to give an appropriate command.
from Tkinter import *
import tkFileDialog
import ttk
import os
indir= '/Users/username/results'
root = Tk()
b = ttk.Combobox(master=root, values=os.listdir(indir)) # This will create a dropdown menu with all the elements in the indir folder.
b.pack()
w = Button(master=root, text='Select', command= ?)
w.pack()
root.mainloop()
I think what you need here is actually a binding. Button not required.
Here is an example that will list everything in your selected directory and then when you click on it in the Combo Box it will print out its selection.
Update, added directory and file name combining to get new full path:
from Tkinter import *
import tkFileDialog
import ttk
import os
indir= '/Users/username/results'
new_full_path = ""
root = Tk()
# we use StringVar() to track the currently selected string in the combobox
current_selected_filepath = StringVar()
b = ttk.Combobox(master=root, values=current_selected_filepath)
function used to read the current StringVar of b
def update_file_path(event=None):
global b, new_full_path
# combining the directory path with the file name to get full path.
# keep in mind if you are going to be changing directories then
# you need to use one of FileDialogs methods to update your directory
new_full_path = "{}{}".format(indir, b.get())
print(new_full_path)
# here we set all the values of the combobox with names of the files in the dir of choice
b['values'] = os.listdir(indir)
# we now bind the Cobobox Select event to call our print function that reads to StringVar
b.bind("<<ComboboxSelected>>", update_file_path)
b.pack()
# we can also use a button to call the same function to print the StringVar
Button(root, text="Print selected", command=update_file_path).pack()
root.mainloop()
Try something like this:
w = Button(master=root, text='Select', command=do_something)
def do_something():
#do something
In the function do_something you create what you need to get the full path. You can also pass vars into the command.
get()
Returns the current value of the combobox.(https://docs.python.org/3.2/library/tkinter.ttk.html)
from Tkinter import *
import tkFileDialog
import ttk
import os
indir= '/Users/username/results'
#This function will be invoked with selected combobox value when click on the button
def func_(data_selected_from_combo):
full_path = "{}/{}".format(indir, data_selected_from_combo)
print full_path
# Use this full path to do further
root = Tk()
b = ttk.Combobox(master=root, values=os.listdir(indir)) # This will create a dropdown menu with all the elements in the indir folder.
b.pack()
w = Button(master=root, text='Select', command=lambda: func_(b.get()))
w.pack()
root.mainloop()
when i run this code in python 3.5:
import tkinter
top = tkinter.Tk()
def callback():
print ("click!")
button = Button(top, text="OK", command=callback)
top.mainloop()
I get an error:
NameError: name 'Button' is not defined
As said, "Button" is not defined
You should try :
import tkinter
top = tkinter.Tk()
def callback():
print ("click!")
button = tkinter.Button(top, text="OK", command=callback)
top.mainloop()
Alternatively to #freidrichen response you can either use (not recommended)
from tkinter import *
or
import tkinter as tk
then
tk.Button(top, text="OK", command=callback)
Importing the tkinter module only gives you access to the module object (tkinter). You can access any of the classes therein by writing e.g. tkinter.Button instead of just Button:
import tkinter
top = tkinter.Tk()
def callback():
print ("click!")
button = tkinter.Button(top, text="OK", command=callback)
top.mainloop()
Or you can specifically import the classes that you need from the module:
import tkinter
from tkinter import Button
top = tkinter.Tk()
def callback():
print ("click!")
button = Button(top, text="OK", command=callback)
top.mainloop()
This is a late response but while browsing the web with a similiar problem, I found this code. Hoping this works out for anyone who encounters this.
if __name__ == '__main__':
try:
from tkinter import *
except ImportError:
from Tkinter import *
Code taken from:
http://code.activestate.com/recipes/577409-python-tkinter-canvas-rectangle-selection-box/
Something like this:
from Tkinter import *
root = Tk()
but = Button(root, text = "button")
but.pack()
#When I try:
but.destroy()
but.pack()
I get an error:
TclError: bad window path name ".37111768"
The pack_forget method will hide the widget and you can pack or grid it again later.
http://effbot.org/tkinterbook/pack.htm
I have managed to get it working :) here is my work:
from Tkinter import *
def changebutton():
but.destroy()
secondbut=Button(root,text="changed")
secondbut.pack()
if __name__=='__main__':
root=Tk()
global but
but= Button(root,text="button",command=changebutton)
but.pack()
root.mainloop()