so I'm starting to learn python and I need to write a script that edits a CSV file. I found this online and had a few questions about what it's exactly doing since the original programmer didn't explain. My question right now though is about syntax. I'm a little confused about some of these lines:
import Tkinter,tkFileDialog
root = Tkinter.Tk()
root.filename = tkFileDialog.askopenfilename(initialdir = "/", title =
"Select a file", filetypes = (("csv files", "*.csv"),))
So my first question is what root equals. I understand I imported two modules called Tkinter and tkFileDialog(Correct me if I'm wrong) into my file. I then created a variable called root and set it equal to a method call?
root = Tkinter.tk()
Next, what does this line do? Is filename a method in one of those modules? I read something about widgets...are widgets methods? As in the word is used interchangeably?
root.filename
Thank you in advance!
You may benefit more form some youtube tutorials on python methods/functions and classes but I can answer your questions in general terms.
So my first question is what root equals.
root is the variable name assigned to the instance that is being created with tkinter.Tk()
This allows you to interact with that instance of tkinter and you can use root to place widgets on the main window of the GUI.
Next, what does this line do? root.filename
root.filename is only a variable name. tkFileDialog.askopenfilename is the class method being used to get the file and assign the file information to the variable name root.filename
So what you are doing here is importing the library tkinter that contains many class methods that can be used to build and manipulate a GUI interface.
note that for an instance of tkinter you will need a mainloop() at the end of your code to make it work. So at the end of your code you will need to have something like root.mainloop() to make sure the program will work as long as everything else is done correctly.
Related
I am a beginner and I am making a login system (just for practicing).
I'm using tkinter to develop a simple UI. The thing is that when I call a second root (sign_in root) with a button from another root (main_screen), and I try to get some values typed in entry with StringVars assigned to them, they return just an empty string ""
def main_screen():
root=Tk()
user=StringVar()
pas=StringVar()
btn2=Button(root,text='Sign-In',command=sign_in_screen)
btn2.place(x=125,y=160)
root.mainloop()
def sign_in_screen():
root1=Tk()
newuser=StringVar()
newpas=StringVar()
ent3=Entry(root1,width=28,textvariable=newuser)
ent3.place(x=100,y=50)
ent4=Entry(root1,width=28,textvariable=newpas,show="*")
ent4.place(x=100,y=100)
btn3=Button(root1,text='Sign-In',command=lambda:register(newuser.get(), newpas.get()))
btn3.place(x=50,y=160)
root1.mainloop()
main_screen()
Having multiple instances of Tk become hideously complicated because each one creates a separate tcl interpreter. This causes weird effects like what you see here. It's you almost always want to use the Toplevel widget.
I am new in tkinter please help me out .
I have implemented a module(PDF2Text.Py) that its class has a function (convert_pdf_to_txt(path)) that takes a path of a pdf file and converts the pdf file into text.
I also implemented another module(TopicModeling.py) that its class has a function (creat_LDA_model(text)) that takes a text and do topic modeling on the text.
Now, I want the tkinter GUI that is, upon clicking the "Browse" button it browses the path with filedialog.askopenfilename and its command function send the given path to convert_pdf_to_txt(path) function of PDF2Text.Py.
Then by clicking the "Model" button, its command function returned text should be sent to creat_LDA_model(text) function in TopicModeling.py and show the result in an Entry widget or any other widget types .
I would like to know the structure of the GUI module;
how to call or get and set the parameters to other modules/functions from the GUI module in command functions of the buttons.
thanks
Seems like you have multiple questions here. I believe one of them is how to start askopenfilename in a particular folder. Once the file name is returned, i can be passed to another function.
fname = askopenfilename(initialdir = r'c:\somepath', filetypes=(("Statement files", "*.pdf"),))
To call other functions you have written, import the module, let's cal it ReadPdf.py, use something like this.
import ReadPdf
PdfOut = ReadPdf.ReadPDFData(fname)
CCStmtTxns = ReadPdf.ReadPDFData(CreditCardPDFDataPathandFile)
I'm trying to create a python program that pulls up a simple window that displays the text "Hello World?" I've imported tkinter and have created a class called MyGUI that should create a simple window. Then I create an instance of the MyGUI class. When I hit "F5" or run the programming after saving it, I get an error:
RESTART: C:....my filepath.....
>>>
Here is the code:
import tkinter
class MyGUI:
def init (self):
# Create the main window widget.
self.main_window = tkinter.tk()
# Create a Label widget containing the
# text 'Hello World!'
self.label = tkinter.Label(self.main_window, text="Hello World!")
# Call the Label widget's pack method.
self.label.pack()
# Enter the tkinter main loop.
tkinter.mainloop()
# Create an instance of the MyGUI class
my_gui = MyGUI()
What causes the "RESTART" error? Does where I save my .py file matter for this program?
Any help would be greatly appreciated. Thanks
The good news:
Your code works (in that it doesn't crash in python3, as is)!
The bad news:
Your code doesn't do anything :( Your only function would raise an exception if called
You have a code-unrelated problem
To resolve problem #1, change init to __init__ and tkinter.tk to tkinter.Tk()
__init__ is the function called by default on instance construction. The underscores are important if you want to override it. The other issue is just a typo.
You're broader problem is... broader. yes it matters where you save your file. If you don't save it in the place you are running python from, you need to supply an absolute path to it, or a relative path from the place you are running from. This is a broad topic, but pretty important and not too challenging. Maybe try here, or any python tutorial.
I don't know what type F5 does on your computer. I would not in general expect it to run python code. Are you in an IDE, then maybe it does run python code? Are you playing call of duty, because then it's more likely to lob a virtual grenade? F5 is app-dependent, probably not a universal binding on your machine
After seeing this, PhotoImage not showing up the image associated with it I wondered why omitting the type of option does not throws an error instead of just not showing up the image?
This code does not show the image without throwing any error
import tkinter as tk
root = tk.Tk()
image1 = tk.PhotoImage("ban.gif")
tk.Label(root,image=image1).pack()
tk.Label(root, text="some string here").pack()
root.mainloop()
But this one works fine
import tkinter as tk
root = tk.Tk()
image1 = tk.PhotoImage(file="ban.gif")
tk.Label(root,image=image1).pack()
tk.Label(root, text="some string here").pack()
root.mainloop()
On effbot it doesn't say anything about it so I checked tcl man page for creating photos but still can not find why it behaves like this.
Also, if those two are duplicate ones, let me know so I will delete/close vote this one.
When you specify a function with named arguments in python, those named arguments appear in a specific order. If you do not supply a name when defining these arguments, they are applied in the order that the arguments appear in the function definition.
In the case of PhotoImage, the first keyword argument is for the name of the image, not a path to a file. So, PhotoImage("ban.gif") is the same as doing PhotoImage(name="ban.gif"). It doesn't throw an error because "ban.gif" is a valid name, and there are use cases where you want to create an image without referencing a file.
im new in this page and i love the answers nice job, with all the help of users, im new on python and wanna make a print of dir in a entry or label doesnt matter for example:
def directory(): os.listdir('/')
files=StringVar()
files.set(directory)
entry=Entry(root, textvariable=files).grid()
obviously in tkinter, the last code make a "print" a list of directories, but make me a list horizontal with this ',' for each folder different, i want it vertical list on this "entry" or "label", suppose later need a scroll bar but, there no is a problem, and make the same for temporal folder on windows look like that...
def directory(): os.listdir('%temp%')
files=StringVar()
files.set(directory)
entry=Entry(root, textvariable=files).grid()
but this %temp% doesnt works directly on python how can i make a listdir of folder?
Since displaying the contents of a directory is going to generally require multiple lines of text, one for each item in it, you have to use atk.Labelortk.Textwidget since a tk.Entrycan only handle a single line of text.
In addition you'll need to convert thelistthat os.listdir() returns into a multi-line string before setting itstextoption. Regardless, in order to be use yourdirectory() function needs to return a value to be useful.
The following code does these basic things and shows how to expand the value of the%temp%environment variable usingos.path.expandvars(). Alternatively, you can get the same value by using thetempfile.gettempdir()function Sukrit recommended.
import os
from Tkinter import *
def directory(folder):
return '\n'.join(os.listdir(folder)) # turn list into multiline string
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
files = directory(os.path.expandvars('%temp%'))
self.label = Label(root, text=files)
self.label.pack(side=LEFT)
root = Tk()
app = App(root)
root.mainloop()