See the previous blog post for an older version of the code:
Python implementation of Fryer with graphical interface
This article is an attempt to "upgrade" the following sections from the old version:
I. Rotation of shapes to simulate four players sitting around a card table facing each other two-by-two
Rotation method rotate(angle) This article only uses the rotation angle of this parameter, the angle of a positive value means counterclockwise rotation; negative value means clockwise rotation.
method rotate in module :
rotate(angle, resample=0, expand=0, center=None, translate=None, fillcolor=None) method of instance
Returns a rotated copy of this image. This method returns a copy of this image, rotated the given number of degrees counter clockwise around its centre.
:param angle: In degrees counter clockwise.
:param resample: An optional resampling filter. This can be one of :py:data: `` (use nearest neighbour), :py:data:`` (linear interpolation in a 2x2 environment), or :py:data:``
(cubic spline interpolation in a 4x4 environment).
If omitted, or if the image has mode "1" or "P", it is set to :py:data: ``. See :ref:`concept-filters`.
:param expand: Optional expansion flag. If true, expands the output image to make it large enough to hold the entire rotated image.
If false or omitted, make the output image the same size as the input image. Note that the expand flag assumes rotation around the center and no translation.
:param center: Optional center of rotation (a 2-tuple). Origin is the upper left corner. Default is the center of the image.
:param translate: An optional post-rotate translation (a 2-tuple).
:param fillcolor: An optional color for area outside the rotated image.
:returns: An :py:class:`~` object.
If it is not a square picture and the rotation angle is not 180 degrees, it will be partially cut off. The effect is as follows:
Demo code:
import tkinter as tk from PIL import Image,ImageTk def load(i=0): img = ("").resize((375,150)) box = (90*i) res = (image=box) () return res if __name__ == '__main__': root = () ('800x480') ('Picture Rotation') cv = (root, width=1600, height=800, bg='darkgreen') () png = [None]*4 coord = ((i,j) for j in (120,345) for i in (200,600)) for i,xy in enumerate(coord): png[i] = load(i) cv.create_image(xy, image=png[i]) cv.create_text(xy[0],xy[1]+95, text=f'turn counterclockwise{i*90}degree (angles, temperature etc)',fill='white') ()
To save the full image before rotating, set a square box box = ((0,0,375,375)).rotate(-90*i), and the effect of clockwise rotation is as follows:
Demo code:
import tkinter as tk from PIL import Image,ImageTk def load(i=0): img = ("").resize((375,150)) box = ((0,0,375,375)).rotate(-90*i) res = (image=box) () return res if __name__ == '__main__': root = () ('800x800') ('Picture Rotation') cv = (root, width=1600, height=800, bg='darkgreen') () png = [] coord = ((i,j) for j in (200,600) for i in (200,600)) for i,xy in enumerate(coord): (load(i)) cv.create_image(xy, image=png[i]) ()
Then use the crop() method to cut out the portion of the image other than the black background, which is the desired image in the four directions of rotation; finally, split these images into small cards again, and store them in a three-dimensional list for later use.
Second, add variables, so that the game has a cumulative process of winning and losing
Add a text box after each player's text box to dynamically display the winners and losers of each game; the value of each player is stored in the global variable Money list, the main code is as follows:
ALL, ONE = 1000, 200 # Initial value, single win/loss value Money = [ALL]*4 #Set initial values for each party ... ... cv.create_text(tx,ty, text=f'Player{x+1}', fill='white') #Player 1-4 Display Text Box (cv.create_text(tx+60,ty, fill='gold',text=Money[x])) #Display box ... ... Money[idx] += ONE*4 #Each time you win ONE*3, add your share. for i in range(4): Money[i] -= ONE # Add more and subtract it here (txt[i], text=str(Money[i])) # Modify the values of the parties ()
Third, the interface to increase the drop-down menu, the menu items called the binding function
The display is shown in the upper left corner of the title image, and the main code is as follows:
btnCmd = 'Deal the cards',dealCards,'Open Cards',playCards,'Shuffle the deck',Shuffle Menu = (root) menu = (Menu, tearoff = False) for t,cmd in zip(btnCmd[::2],btnCmd[1::2]): menu.add_radiobutton(label = t, command = cmd) menu.add_separator() #Menu Divider menu.add_command(label = "Exit.", command = ExitApp) Menu.add_cascade(label="Menu.",menu = menu) (menu = Menu)
IV. Importing the infobox library and increasing the use of prompt infoboxes
Two types of infoboxes are used: the prompt showinfo() and the confirmation selection askokcancel().
The library has a total of 8 infobox types, which are used in basically the same way, only the icons displayed are different:
Help on module in tkinter:
NAME
FUNCTIONS
askokcancel(title=None, message=None, **options)
Ask if operation should proceed; return true if the answer is ok
askquestion(title=None, message=None, **options)
Ask a question
askretrycancel(title=None, message=None, **options)
Ask if operation should be retried; return true if the answer is yes
askyesno(title=None, message=None, **options)
Ask a question; return true if the answer is yes
askyesnocancel(title=None, message=None, **options)
Ask a question; return true if the answer is yes, None if cancelled.
showerror(title=None, message=None, **options)
Show an error message
showinfo(title=None, message=None, **options)
Show an info message
showwarning(title=None, message=None, **options)
Show a warning message
DATA
ABORT = 'abort'
ABORTRETRYIGNORE = 'abortretryignore'
CANCEL = 'cancel'
ERROR = 'error'
IGNORE = 'ignore'
INFO = 'info'
NO = 'no'
OK = 'ok'
OKCANCEL = 'okcancel'
QUESTION = 'question'
RETRY = 'retry'
RETRYCANCEL = 'retrycancel'
WARNING = 'warning'
YES = 'yes'
YESNO = 'yesno'
YESNOCANCEL = 'yesnocancel'
Also: whether the deal, open, shuffle buttons can be clicked or not is controlled by two global variables that pop up a message box when they can't be used. But a better way is usually to set the state of the buttons to toggle between and , using the following code:
if btn[0]['state'] == : btn[0]['state'] = else: btn[0]['state'] = # Makes the button grayed out and unable to be pressed # or used at the initial button: (root,text="I can't.",command=test,width=10,state=)
The complete source code for "Fraud".
Run results:
This article on the implementation of Python with a graphical interface to the game of fried gold (upgraded version) of the article is introduced to this, more related Python fried gold content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!