I have a python file which is called from other files
Every time the python file is imported and mainApp is called from others, the tkinter button inside the python file is executed automatically.
here is part of the python file code
from Tktable import *
def exp(Output):
import csv
from tkFileDialog import askdirectory
folder=askdirectory();
if folder:
path = folder+'/outputTable.csv';
file = open(path, 'w');
writer = csv.writer(file)
title = ['Premise','Conclusion','Support','Confidence','Lift']
writer.writerow(title);
zip(*Output)
for item in zip(*Output):
writer.writerow(item)
file.close()
def mainApp(Output):
from Tkinter import Tk, Label, Button, Scrollbar, Frame
root = Tk()
top = Frame(root).pack(side = 'top', fill = 'x')
...
export = Button(top, text='EXPORT', command=exp(Output))
export.grid(row=0, column=4, sticky = 'e')
...
How could I stop the auto execution of the button? And why is this happening? Can anyone help me? Thank you!
It happens because you're calling the function. Pass it a function object instead, such as one created with lambda.
..., command=(lambda: exp(Output)))
Related
Using Tkinter, os, and, pygame modules im finding all files and making a button to play the file because it will be an mp3 file but every time the function overwrites itself so I want to be able to write a function but inside the command parameter in the Tkinter button so it isn't a function but it operates like one
the code i already have:
import os
import pygame
import tkinter as tk
dir_path = os.path.dirname(os.path.realpath(__file__))
d = dir_path+"\\mp3s"
root = tk.Tk()
frame=tk.Frame(root)
root.title("title")
frame.pack()
for path in os.listdir(d):
full_path = os.path.join(d, path)
full_name = os.path.basename(full_path)
def playsong():
pygame.init()
pygame.mixer.music.load(full_path)
pygame.mixer.music.play()
play = tk.Button(frame,
text=full_name,
command=playsong)
play.pack()
root.mainloop()
the function inside the for statement is getting overwritten and I knew this would happen but I was still going to try this works for one file but I want a bunch of different files inside of the folder named "mp3s"
the rest of the code works this is the part that does not
def playsong():
pygame.init()
pygame.mixer.music.load(full_path)
pygame.mixer.music.play(
I wanted to run multiple functions in a line more specifically than lines and I figured out how to do this using lambda
import tkinter as tk
root = tk.Tk()
frame=tk.frame(root)
button = tk.Button(frame, text="name", command= lambda: [func1(), func2()])
I am currently messing about with Tkinter creating an interface for a project I created. The program takes in a bunch of file paths as inputs for it to run. I'm trying to create a tkinter interface where I can upload the 4 files I need or at least somehow get the filepaths and the. feed those to the program. Here is what I have:
import sys
import os
import comparatorclass
from tkinter import *
from tkinter.ttk import *
from tkinter.filedialog import askopenfile
root=Tk()
root.geometry('1000x1000')
def open_file():
file = askopenfile(mode ='r', filetypes =[('Python Files', '*.py')])
if file is not None:
content = file.read()
print(content)
def run_comparator():
comparatorclass.main()
button2 = Button(root, text ='Open', command = lambda:open_file())
button2.pack(side = TOP, pady = 10)
button1 = Button(root,text="hello",command= run_comparator)
button1.pack()
root.mainloop()
as you can see, I have two buttons. The issue I'm having is how to connect my openfile function to my run_comparator function such that the 4 files I need to open are passed on to the run_comparator
This is for my assignment.
I was given a template like this... def_choosefile() :
import tkinter
from tkinter import filedialog
root_window = tkinter.Tk()
root_window.withdraw()
return filedialog.askopenfilename()
So if i get this correct, it'll prompt a dialog window, asking to select a file. And when a file is selected, the program is supposed to tell what files did it pick. Using these:
selected_file = choose_file()
print ("Selected file: {}".format (selected_file))
after that, how do i make the program read the files normally? Normally i would use:
infile = open('text')
infile.readline()
import tkinter
from tkinter import filedialog
root_window = tkinter.Tk()
root_window.withdraw()
def choose_file():
return filedialog.askopenfilename()
selected_file = choose_file()
with open (selected_file,'r') as readfile:
lines = readfile.read().splitlines()
for line in lines[0:2]:
print (line)
I am interested to create a single tkinter GUI in which i can define a path to run a python script located in a particular folder. The code is shown below. It can read the required .py file from the set of files in that folder using the path i have given and open the dialogue box for plot graphs too but doesnt do anything. When i click the plot graphs button rather it gives an error "AttributeError: 'int' object has no attribute 'display_graph'". Can anyone check and edit my code to help.(I am using spyder so tk is tkr). I know about py2exe, So i would appreciate if someone can help with tkinter GUI code. Thanks
My python script is Empdata.py and i used def display_graph(data) in it:
Code
import glob
import tkinter as tkr
import os
path = r'C:\Users\C253271\Desktop\Empower data\\'
allpyfiles =glob.glob(os.path.join(path, "*.py"))
for file in allpyfiles:
file =('Empdata')
def graph():
global v
file.display_graph(v.get())
root = tkr.Tk()
v = tkr.StringVar()
tkr.Button(root, text='Close',command=root.destroy).grid(row=2, column=1)
tkr.Button(root, text='Plot Graphs', command = graph).grid(row=2, column=0)
root.mainloop()
In the code from the question, file is a string. It thus does not have any methods. What you really want to do is to import the python file, such that its content is available.
To do so, you may use importlib
f = importlib.import_module("Empdata") # if you have Empdata.py in your folder
f.some_method()
Your example should therefore look something like
import Tkinter as tkr
import importlib
fn = "Empdata"
f = importlib.import_module(fn)
def graph():
global v
f.display_graph(v.get())
root = tkr.Tk()
v = tkr.StringVar()
tkr.Button(root, text='Close',command=root.destroy).grid(row=2, column=1)
tkr.Button(root, text='Plot Graphs', command = graph).grid(row=2, column=0)
root.mainloop()
Hi i have some python scripts. I need to create a GUI with few buttons, to open those files by browsing. I have to run those scripts just by clicking the GUI button, and should write the output into a n excel sheet. I tried with the below code, but with that i can just read the file! please help?
Thank you
from Tkinter import *
from tkFileDialog
import askopenfilename
def callback():
r = open(askopenfilename(),'r')
a = Button(text='click me', command=callback)
a.pack()
mainloop()
inside the callback() instead of opening the file, execute the file using execfile("abc.py")
def callback():
abc = askopenfilename()
execfile("abc.py")
This code below is a simple example that reads each line from the selected input file and then writes it to a new excel file called output.xls. It use the excellent xlwt library for creating the excel file. You could also chose to create a csv file using the csv module that is part of the python standard library which is also readable by excel. The advantages of using xlwt is that you can apply formating, merge cells, formulas, and so many other features that are part of the xls file format.
import xlwt
from Tkinter import *
from tkFileDialog import askopenfilename
def callback():
filename = askopenfilename()
wb = xlwt.Workbook()
ws0 = wb.add_sheet('Sheet1')
with open(filename, 'r') as f:
for i, line in enumerate(f):
ws0.write(i, 0, line.strip())
wb.save('output.xls')
errmsg = 'Error!'
a = Button(text='click me', command=callback)
a.pack()
mainloop()
import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
dirname=tkFileDialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
do something with dirname
print dirname
#This code for file selection:
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)