Why does this Python script print None
Dialogues and messages (message boxes)
introductionTkinter (and of course TK) provide a number of dialogs that can be used to generate messages (message boxes) during the ablation process, which inform about warnings or errors, but also windows with which a user files, Can choose directories or colors. There are also dialogs with which it is possible to read in strings, whole numbers or floating point numbers.
Let's take a look at the typical GUI-controlled process with dialogues and messages. We may have a button with which we start a dialog. This could, for example, be the "quit" button in the following window:
If you click on the "quit" button, a verify window is started:
Let us assume that we want to warn the users that the "Quit" functionality is not yet implemented. In this case we can use the "warning" message:
If someone presses the "No" button, the "Cancel" message appears:
Let's come back to our first dialog with the "quit" and the "answer" buttons. If the "Answer" functionality has not yet been implemented, it might make sense to display the following error message:
Python script that implements all of the previous dialogs:
from Tkinter import * from tkMessageBox import * def answer (): showerror ("Answer", "Sorry, no answer available") def callback (): if askyesno ('Verify', 'Really quit?'): showwarning ('Yes ',' Not yet implemented ') else: showinfo (' No ',' Quit has been canceled ') Button (text =' Quit ', command = callback) .pack (fill = X) Button (text =' Answer ', command = answer) .pack (fill = X) mainloop ()
Messages (Message Boxes)The messages in Tkinter are made available with the tkMessageBox module.
tkMessageBox consists of the following functions, which correspond to the dialog windows:
- askokcancel (title = None, message = None, ** options)
Asks with "OK" and "cancel" whether an action should be carried out further; returns "true" if "OK" is clicked, otherwise (cancel) returns false.
- askquestion (title = None, message = None, ** options)
Asks a question
- askretrycancel (title = None, message = None, ** options)
Asks with "YES" and "CANCEL" whether an action should be tried again. Returns "true" if "YES" is clicked, otherwise (cancel) false is returned.
- askyesno (title = None, message = None, ** options)
Asks for with "YES" and "NO". Returns "true" if "YES" is clicked, false if "NO" is clicked.
- askyesnocancel (title = None, message = None, ** options)
Asks for with "YES", "NO" and "CANCEL". Returns "true" if "YES" is clicked, "false" if clicked on "NO" and "None" if clicked on "CANCEL"
- showerror (title = None, message = None, ** options)
Generates a window with an error message
- showinfo (title = None, message = None, ** options)
Generates an info message
- showwarning (title = None, message = None, ** options)
Generates a warning
Dialog for opening a file / Open File DialogueThere is hardly a serious application that does not read or write to a file in some form. In addition, it is often necessary for the user to select a directory. Tkinter provides the tkFileDialog module for this purpose.
from Tkinter import * from tkFileDialog import askopenfilename def callback (): name = askopenfilename () print name errmsg = 'Error!' Button (text = 'File Open', command = callback) .pack (fill = X) mainloop ()
The above code creates a window with a single button that contains the text "File Open". If the button is pressed, the following window appears:
The appearance of the file-open dialog depends on the GUI of the operating system used. The above example was created under Linux as the operating system and Gnome as the desktop. If we start the same program under Windows 7, it looks like this:
Color selectionThere are applications in which it is necessary or useful for a user to select one or more colors. Of course, Tkinter also offers a ready-made method for this. The tkColorChooser module offers the askColor method: result = tkColorChooser.askColor (color, option = value, ...) If the user clicks the OK button in the pop-up window, the askColor () method returns a tuple with two elements as return value, ((106, 150, 98), '# 6a9662')
The first element of the tuple return  is in turn a tuple (R, G, B) with the selected color in RGB notation in decimal values between 0 and 255. The second element return  is the hexadecimal representation of the selected color.
If the user clicks on "Cancel", the method returns the tuple (None, None).
The optional parameters are:
|color||The default color can be selected with the optional "color" parameter. The default value is gray.|
|title||Contains the text that appears as the title in the pop-up window. The default value is "Color".|
|parent||The pop-up window should appear above the window W. By default it appears above the root window.|
Another simple example: from Tkinter import * from tkColorChooser import askcolor def callback (): result = askcolor (color = "# 6A9662", title = "Bernd's Color Chooser") print result root = Tk () Button (root, text = 'Choose Color', fg = "darkgreen", command = callback) .pack (side = LEFT, padx = 10) Button (text = 'Quit', command = root.quit, fg = "red"). Pack (side = LEFT, padx = 10) mainloop () Here, too, the look-and-feel depends on the operating system and the desktop used. Appearance under Linux with Gnome:
If we start the same program under Windows 7, it looks like this:
- What is Donald Trump's hair care product
- Which is the best book for doctors
- What are the famous marketplaces in Alwar
- Why can't sound travel faster
- What is India's largest product
- What does funeral insurance cover
- What are some examples of collaborative painting
- What is the history of online dating
- Can laziness be a strength
- How do I duplicate a wordpress page
- What are some free SSAT prep resources
- Were the Saturn V rocket parts reusable
- Which river is near Calcutta?
- What is London's version of the Hamptons
- Why do dolphins swim near the shore?
- Are boots with high heels easy to walk on
- Does Python support operator overloading
- Was John Updike a homophobe
- What is Samsung Flow
- What is a complete valence shell
- Why do nice people have mean friends
- Where are the best pierogis in Boston
- Is patchwork the same as quilting
- What are the best substitutes for green onions