Think Python How to Think Like a Computer Scientist
Download 1.04 Mb. Pdf ko'rish
|
thinkpython
- Bu sahifa navigatsiya:
- Exercise 19.2
19.3. Canvas widgets
185 Exercise 19.1 Write a program that creates a GUI with a single button. When the button is pressed it should create a second button. When that button is pressed, it should create a label that says, “Nice job!”. What happens if you press the buttons more than once? You can see my solution at thinkpython. com/code/button_demo.py 19.3 Canvas widgets One of the most versatile widgets is the Canvas, which creates a region for drawing lines, circles and other shapes. If you did Exercise 15.4 you are already familiar with canvases. The method ca creates a new Canvas: canvas = g.ca(width=500, height=500) width and height are the dimensions of the canvas in pixels. After you create a widget, you can still change the values of the options with the config method. For example, the bg option changes the background color: canvas.config(bg='white') The value of bg is a string that names a color. The set of legal color names is different for different implementations of Python, but all implementations provide at least: white black red green blue cyan yellow magenta Shapes on a Canvas are called items. For example, the Canvas method circle draws (you guessed it) a circle: item = canvas.circle([0,0], 100, fill='red') The first argument is a coordinate pair that specifies the center of the circle; the second is the radius. Gui.py provides a standard Cartesian coordinate system with the origin at the center of the Canvas and the positive y axis pointing up. This is different from some other graphics systems where the the origin is in the upper left with the y axis pointing down. The fill option specifies that the circle should be filled in with red. The return value from circle is an Item object that provides methods for modifying the item on the canvas. For example, you can use config to change any of the circle’s options: item.config(fill='yellow', outline='orange', width=10) width is the thickness of the outline in pixels; outline is the color. Exercise 19.2 Write a program that creates a Canvas and a Button. When the user presses the Button, it should draw a circle on the canvas. |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling