Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
Where can I find the most modern tutorial that teaches tkinter together with ttk?
Tkinter seems the only way to go in Python 3 (don't suggest Python 2), and ttk gave me hope for good-looking GUI.
I have found the TkDocs tutorial to be very useful. It describes building Tk interfaces using Python and Tkinter and ttk and makes notes about differences between Python 2 and 3. It also has examples in Perl, Ruby and Tcl, since the goal is to teach Tk itself, not the bindings for a particular language.
I haven't gone through the whole thing from start to finish, rather have only used a number of topics as examples for things I was stuck on, but it is very instructional and comfortably written. Today reading the intro and first few sections makes me think I will start working through the rest of it.
Finally, it's current and the site has a very nice look. He also has a bunch of other pages which are worth checking out (Widgets, Resources, Blog). This guy's doing a lot to not only teach Tk, but also to improve people's understanding that it's not the ugly beast that it once was.
I recommend the NMT Tkinter 8.5 reference.
Themed widgets
Customizing and creating ttk themes and styles
Finding and using themes
Using and customizing ttk styles
The ttk element layer
The module names used in some examples are those used in Python 2.7.
Here's a reference for the name changes in Python 3: link
One of the conveniences of ttk is that you can choose a preexisting theme,
which is a full set of Styles applied to the ttk widgets.
Here's an example I wrote (for Python 3) that allows you to select any available theme from a Combobox:
import random
import tkinter
from tkinter import ttk
from tkinter import messagebox
class App(object):
def __init__(self):
self.root = tkinter.Tk()
self.style = ttk.Style()
available_themes = self.style.theme_names()
random_theme = random.choice(available_themes)
self.style.theme_use(random_theme)
self.root.title(random_theme)
frm = ttk.Frame(self.root)
frm.pack(expand=True, fill='both')
# create a Combobox with themes to choose from
self.combo = ttk.Combobox(frm, values=available_themes)
self.combo.pack(padx=32, pady=8)
# make the Enter key change the style
self.combo.bind('<Return>', self.change_style)
# make a Button to change the style
button = ttk.Button(frm, text='OK')
button['command'] = self.change_style
button.pack(pady=8)
def change_style(self, event=None):
"""set the Style to the content of the Combobox"""
content = self.combo.get()
try:
self.style.theme_use(content)
except tkinter.TclError as err:
messagebox.showerror('Error', err)
else:
self.root.title(content)
app = App()
app.root.mainloop()
Side note: I've noticed that there is a 'vista' theme available when using Python 3.3 (but not 2.7).
I recommend reading the documentation. It is simple and authoritative, and good for beginners.
It's not really fresh but this is concise, and from what I've seen valid either for Python 2 and 3.
Related
I want to create a terminal like design using tkinter. I also want to include terminal like function where once you hit enter, you would not be able to change your previous lines of sentences. Is it even possible to create such UI design using tkinter?
An example of a terminal design may look like this:
According to my research, i have found an answer and link that may help you out
Firstly i would like you to try this code, this code takes the command "ipconfig" and displays the result in a new window, you can modify this code:-
import tkinter
import os
def get_info(arg):
x = Tkinter.StringVar()
x = tfield.get("linestart", "lineend") # gives an error 'bad text index "linestart"'
print (x)
root = tkinter.Tk()
tfield = tkinter.Text(root)
tfield.pack()
for line in os.popen("ipconfig", 'r'):
tfield.insert("end", line)
tfield.bind("<Return>", get_info)
root.mainloop()
And i have found a similar question on quora take a look at this
After asking for additional help by breaking down certain parts, I was able to get a solution from j_4321 post. Link: https://stackoverflow.com/a/63830645/11355351
Okay so I'm a teacher using tkinter to create gui's with my students. My mac users are fine with the look but in windows their programs leave something to be desired. I don't have access to PIP (my sys admin WONT open it up for the kids to use terminal) so I'm limited in my libraries.
Im (slowly) building up some configurations that i think would help them get the idea behind how to configure (I explain its like adding CSS to HTML - doens't (usually) change the function, it just looks better) any tips or examples i can pass on to them would be super helpful. Here is an example of a slider i turned into a tactile on/off switch.
import tkinter as tk
main = tk.Tk()
def on_off_slider_action(slider_val):
if int(slider_val): #using as "truthy"
on_off_slider.configure(troughcolor="#00FF00")
else:
on_off_slider.configure(troughcolor="#FF0000")
on_off_slider = tk.Scale(main, from_=0,
to=1,orient=tk.HORIZONTAL,length=50,showvalue=0,
command=on_off_slider_action,width=30)
on_off_slider.configure(bd=0)
on_off_slider.configure(relief=tk.FLAT)
on_off_slider.configure(sliderrelief=tk.FLAT)
on_off_slider.configure(bg="#999999", activebackground="#888888" )
on_off_slider.grid(row=0,column=0)
main.mainloop()
example switch off
example switch on
I hope you will understand my question.
I noticed that I get the same results when I import turtle module in following two ways.
from turtle import Turtle
t=Turtle()
t.screen.bgcolor("black")
and also
import turtle
turtle.bgcolor("black")
I am confused about this, “from turtle import Turtle”.
According to what I know, it means “to import Turtle.py from turtle (folder / package)”. I may be wrong, you can help me out to understand better.
But I can’t find any Turtle.py module. It is only turtle.py I saw.
What's weird about it is that it works.
Can anyone tell me why?
I am using Python version 3.6
Python's turtle.py is unusual in that it presents both a function-based interface and an object-oriented interface. Depending on how you import it, you can work with one, or the other, or both.
Here, we are using the object-oriented interface to invoke the screen method bgcolor():
from turtle import Turtle
t = Turtle()
t.screen.bgcolor("black")
I usually write this as:
from turtle import Turtle, Screen
screen = Screen()
screen.bgcolor("black")
t = Turtle()
as having direct access to the Screen object simplifies things. Using this style import, you cannot access the function-based interface.
When we do this simpler import, we have access to both the function-based interface and the object-oriented interface. Here, we're using the function bgcolor() to set the background color:
import turtle
turtle.bgcolor("black")
Using either the function-based or object-oriented interface to turtle.py is fine, but you can get yourself seriously confused when mixing the two.
I am trying to make a simple GUI with some basic options of GPG in order to make it easier for new people to use this tool. I need to place a terminal in my GUI, so that trough the GUI the user can click buttons which activate commands in the terminal. With this program people could get used to see the commands working and I avoid the problem of the introduction of the passwords. I want this program to be free software, so that if anyone who helps wants to complete it and share it, please feel free to doing so (well, I would like to see it working correctly firstxD)
I have searched this things but the only answer which looked useful for me does not work (when I click the button of send it simlpy does nothing). I leave here the link to the this other question and my code:
Link: Giving a command in a embedded terminal
My code:
from tkinter import *
import tkinter.ttk as ttk
import os
def cifrado():
archivo=filebox.get()
algoritmo=algo_selec.get()
orden='gpg -ca --cipher-algo '+str(algoritmo)+' '+str(archivo)
os.system(orden) #I need to run this on the terminal
window=Tk()
window.title('GPG Gui')
window.geometry("600x600")
notebook=ttk.Notebook(window)
notebook.pack(fill='both', expand='yes')
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
notebook.add(frame1, text='Cifrados simétricos')
notebook.add(frame2, text='Cifrados asimétricos')
#Symmetric encryption part
filelabel=Label(frame1,text='Archivo: ')
filelabel.grid(row=1,column=1)
filebox=Entry(frame1)
filebox.grid(row=1,column=2)
algolabel=Label(frame1,text='Algoritmo: ')
algolabel.grid(row=2,column=1)
algo_selec=ttk.Combobox(frame1,values=["IDEA","3DES","CAST5","BLOWFISH","AES","AES192","AES256","TWOFISH","CAMELLIA128","CAMELLIA192","CAMELLIA256"]) #DESPLEGABLE
algo_selec.set("AES256")
algo_selec.configure(width=18)
algo_selec.grid(row=2,column=2)
keylabel=Label(frame1,text='Contraseña: ')
keylabel.grid(row=3,column=1)
keybox=Entry(frame1,show="*",width=20)
keybox.grid(row=3,column=2)
b1=Button(frame1,text="Cifrar",command=lambda:cifrado())
b1.grid(row=1,column=3)
#Well, I need to write here the code of the terminal and connect it to the 'cifrado()' function
#Asymmetric encryption
window.mainloop()
My understanding is that the standard set-up for a tkinter program starts off like this:
from tkinter import *
from tkinter import ttk
I understand that tkinter is a package, but if I've already imported all with the *, why do I still need to import ttk? Why do I get an error if I take out the second line and try to reference ttk?
When you do from some_package import *, python will import whatever that package chooses to export. What it chooses to export may be a subset of what is actually stored in the package folder. Why is that? There's no particular reason, it's just how the package author decided to do things.
This information about what to export is defined in the __init__.py file that is inside the package (in this case, tkinter/init.py). If you look at that file you'll notice that it doesn't import ttk itself, thus ttk won't be exported and therefore can't be imported with a wildcard import.
Again, there's no particular reason other than that's how the authors of tkinter and ttk chose to do things.
For more information on the mechanics of packaging, see the packaging portion of the python tutorial (https://docs.python.org/3/tutorial/modules.html#packages)
The better way to import tkinter
You may think it's standard because many tutorials do it that way, but it's generally a bad practice. The better way, IMO, is to give the tkinter library an explicit name:
# python 3.x
import tkinter as tk
from tkinter import ttk
# python 2.x
import Tkinter as tk
import ttk
This will make your code considerably easier to read, because you have to explicitly state which toolkit you are using:
b1 = tk.Button(...) # uses a standard tk button
b2 = ttk.Button(...) # uses a ttk button
I can think of no good reason to do it any other way. Doing a global import saves you a couple of bytes each time you call a tkinter function, but at the expense of clarity. Plus, it reinforces a bad practice that might bleed into how you use other libraries.
The real authority, IMO, is PEP8, which has this to say on the matter:
Wildcard imports (from import *) should be avoided as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance).
Because tkinter/__init__.py doesn't import ttk, so ttk isn't included in from tkinter import *.
Briefly: from tkinter import * imports from file/packet tkinter but it doesn't mean that it will import from file/packet tkinter.ttk
what the other two answers here failed to state is that ttk is not imported because it is a submodule within the tkinter module, effectively a module in itself.
so when you import tkinter you get all of the parts that directly belong to tkinter
but ttk does not directly belong and so must be imported explicitly.
however Bryan Oakley makes a good point that importing everything from a module into local namespace (as many newbies do) can lead to big problems later on when you start to use more modules. this is because some of these modules may share function names even though the functions themselves may do completely different things.
it is always best for large modules to do:
import module as mod
or
import module
and then reference the functions as belonging to the modules namespace:
module.function()
this gives you more control over what you are doing and makes it clearer later on what the function actually belonged to.