Python 实现购物商城,含有用户入口和商家入口

这是模拟淘宝的一个简易的购物商城程序。

用户入口具有以下功能:

  1. 登录认证
    1. 可以锁定用户
    2. 密码输入次数大于3次,锁定用户名
    3. 连续三次输错用户名退出程序
  2. 可以选择直接购买,也可以选择加入购物车
  3. 用户使用支付密码完成支付,支付密码连续输入错误达3次,锁定用户名

商家入口具有以下功能:

  1. 登录认证
    1. 可以锁定用户
    2. 密码输入次数大于3次,锁定用户名
    3. 连续三次输错用户名退出程序
  2. 商家可以编辑商品
    1. 上架新品
    2. 下架商品
    3. 修改商品信息:商品名、单价、库存

每个用户的用户名、密码、余额、支付密码,以行记录定义在 user_list.txt 文件中,以逗号分隔;

每件商品的商品名、单价、库存,以行记录定义在 product_list.txt 文件中,以逗号加一个空格分隔;

被锁定用户名记录在 lock_list.txt 文件中,以行分隔;

商家的用户名、密码定义在 seller_list.txt 文件中,以逗号分隔;

 

  1 # Joe Young
  2 
  3 import getpass
  4 import os
  5 
  6 # 调用os模块的system方法传入'cls'参数,清屏
  7 os.system('cls')    
  8 
  9 while True:
 10     entrance = input('请选择:\n\t1. 用户登陆\n\t2. 商家登陆\n>>>')
 11     if entrance != '1' and entrance != '2':
 12         print('\n输入有误,请重试...\n')
 13     else:
 14         break
 15 
 16 # 打印商品列表
 17 def print_product_list():
 18     index = 1
 19     with open('product_list.txt', 'r') as product_file:
 20         for product_line in product_file:
 21             L = [commodity, price, stock] = product_line.strip('\n').split(', ')
 22             commodity_list.append(L)
 23             print((str(index) + '. ' + commodity).ljust(20) + ('单价:' + price + '').ljust(15) + '库存:' + stock)
 24             index += 1
 25     return
 26 
 27 # 用户入口
 28 if entrance == '1':
 29 
 30     info = []          # 存放用户的信息,初始为空
 31     if_payed = True    # if_payed 表示订单是否已支付
 32     username = ''
 33 
 34     # 登录接口
 35     count = 0
 36     while count < 3:
 37         username = input('\n用户名: ')
 38 
 39         # 打开锁定列表文件
 40         with open('lock_list.txt', 'r+') as lock_file:  
 41             for lock_line in lock_file:
 42                 # 用户名在锁定名单里面,则退出程序
 43                 if username == lock_line.strip('\n'):   
 44                     exit('\n用户名 %s 已被锁定,请联系管理员...' % username)
 45 
 46         login = False   # 登录标志,初始为False
 47 
 48         # 打开用户名列表文件,读权限
 49         user_file = open('user_list.txt', 'r')     
 50 
 51         for user_line in user_file:
 52             # 获取每行的用户信息,用户名、密码、余额、支付密码,存入info列表
 53             info = [user, passwd, balance, pay_passwd] = user_line.strip('\n').split(',')
 54             # 用户名匹配,则进入密码输入环节
 55             if user == username:    
 56                 n = 0
 57                 # 3次输入机会
 58                 while n < 3:    
 59                     password = getpass.getpass('密码: ')
 60                     # 密码匹配,显示登录成功
 61                     if passwd == password:  
 62                         print('\n欢迎 %s 登录商城,祝您购物愉快!\n' % username)
 63                         login = True        # 登录标志赋值为True
 64                         break
 65                     # 密码不匹配
 66                     else:   
 67                         # n = 2 时是最后一次机会,不必提示还剩下0次机会
 68                         if n != 2:  
 69                             print('\n密码错误,请重新输入,您还有 %d 次机会\n' % (2-n))
 70                     n += 1
 71                 # 密码错误次数达到3次,锁定用户名,退出程序
 72                 else:   
 73                     open('lock_list.txt', 'w').write(username + '\n')
 74                     exit('\n错误次数过多,账户已被锁定...')
 75 
 76                 # 登录成功,跳出for循环
 77                 if login:   
 78                     break
 79         else:
 80             if count != 2:
 81                 print('\n用户名不存在,请重试,您还有 %d 次机会' % (2-count))
 82 
 83         user_file.close()
 84 
 85         count += 1
 86 
 87         # 登录成功,跳出while循环
 88         if login:   
 89             break
 90 
 91     else:
 92         exit('\n错误次数过多,程序已退出...')
 93 
 94     # 购买程序
 95     shopping_cart = []  # 购物车初始为空
 96     commodity_list = []
 97 
 98     print_product_list()
 99 
100     while True:
101         i = input('\n请选择商品(输入序号),或输入 c 取消购买:')
102 
103         if i == 'c':
104             while True:
105                 a = input('\n是否继续购买?(Y/N):')
106                 if a == 'n' or a == 'N':
107                     exit('\n交易结束...')
108                 elif a == 'y' or a == 'Y':
109                     break
110                 else:
111                     print('\n输入格式有误,请重试...')
112                     continue
113 
114         if not i.isdigit():
115             print('\n输入格式有误,请重试...')
116             continue
117 
118         i = int(i)
119 
120         if i <= 0 or i > len(commodity_list):
121             print('\n此商品不存在,请重试...')
122             continue
123 
124         item_name = commodity_list[i-1][0]      # 商品名称
125         item_price = commodity_list[i-1][1]     # 商品价格
126         item_stock = commodity_list[i-1][2]     # 商品库存
127 
128         print('\n您已选择了 %s ,请输入购买的数量,或输入 b 重新选择:' % item_name)
129 
130         back = False
131 
132         while True:
133             num = input('>>>')
134             if num == 'b':
135                 back = True
136                 break
137             if not num.isdigit():
138                 print('输入格式有误,请重试...')
139                 continue
140             if int(num) > int(item_stock):
141                 print('数量大于库存,请重试...')
142                 continue
143             if int(num) == 0:
144                 print('数量应大于0,请重试...')
145             break
146         if back:
147             continue
148 
149         item = [item_name, item_price, num]
150 
151         print('\n您已选择了 %s,单价:%s 元,数量:%s,您想立即购买还是加入购物车?\n' % (item_name, item_price, num))
152         print('\t1. 立即购买\n\t2. 加入购物车\n')
153 
154         while True:
155             choice = input('>>>')
156             if not (choice == '1' or choice == '2'):
157                 print('输入有误,请重试...')
158                 continue
159             break
160 
161         user_balance = int(info[2])
162 
163         # 立即购买
164         if choice == '1':   
165             amount = int(item_price) * int(num)
166             count = 0
167             cancel = False
168 
169             while count < 3:
170                 user_pay_passwd = getpass.getpass('\n请输入支付密码,或输入 c 放弃支付:')
171                 if user_pay_passwd == 'c':
172                     print('\n取消支付成功...')
173                     cancel = True
174                     break
175                 elif user_pay_passwd != info[3]:
176                     if count != 2:
177                         print('\n密码错误,请重试,您还有 %d 次机会...' % (2-count))
178                     count += 1
179                 else:
180                     break
181 
182             if count == 3:
183                 with open('lock_list.txt', 'w') as lock_file:
184                     lock_file.write(username + '\n')
185                 exit('密码错误,账户已被锁定...')
186 
187             if cancel:
188                 while True:
189                     choice = input('\n是否继续购买?(Y/N):')
190                     if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'):
191                         print('\n输入格式有误,请重试...')
192                         continue
193                     break
194                 if choice == 'Y' or choice == 'y':
195                     continue
196                 else:
197                     break
198 
199             # 如果用户的账户余额大于总金额
200             if user_balance >= amount:  
201                 user_balance -= amount
202                 print('\n支付成功!您已成功购买 %s ,单价:%s 元,数量:%s,总金额:%s 元,账户余额:%s 元'
203                       % (item_name, item_price, num, amount, user_balance))
204                 lines = open('product_list.txt', 'r').readlines()
205                 # 定位到用户所购买的商品所在行,分割成列表赋值给select
206                 select = lines[i-1].strip('\n').split(', ')             
207                 # 修改商品的库存
208                 select[-1] = (str(int(select[-1]) - int(num)) + '\n')   
209                 # 拼接成字符串
210                 lines[i-1] = ', '.join(select)                          
211                 # 将修改写回文件
212                 open('product_list.txt', 'w').writelines(lines)         
213 
214                 lines = open('user_list.txt', 'r').readlines()
215                 # 修改用户余额
216                 for line in lines:  
217                     if username in line.split(','):     # 定位到用户名所在行
218                         j = lines.index(line)           # 获取用户名所在行的行号索引
219                         select = line.split(',')        # 分割用户名所在行赋值给列表select
220                         select[-2] = str(user_balance)  # 修改用户余额
221                         lines[j] = ','.join(select)     # 修改后的列表拼接成字符串,覆盖用户名所在行
222                         open('user_list.txt', 'w').writelines(lines)    # 将修改写回文件
223             else:
224                 print('\n对不起,您的余额不足...')
225 
226         else:   # 加入购物车
227             j = 0
228             for j in range(len(shopping_cart)):
229                 # 如果商品在购物车里面,更新商品数量
230                 if item_name in shopping_cart[j]:   
231                     shopping_cart[j][2] = str(int(shopping_cart[j][2]) + int(num))
232                     break
233             # 商品若不在购物车,则添加到购物车
234             else:
235                 shopping_cart.append(item)  
236             print('\n成功加入购物车!')
237 
238         while True:
239             choice = input('\n是否继续购买?(Y/N):')
240             if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'):
241                 print('\n输入格式有误,请重试...')
242                 continue
243             break
244 
245         if choice == 'Y' or choice == 'y':
246             continue
247         else:
248             break
249 
250     # 如果购物车不为空
251     if shopping_cart:   
252         print('\n您的购物车里有以下宝贝:\n')
253         i = 1
254         total_sum = 0
255         for item in shopping_cart:
256             (commodity, price, number) = (item[0], item[1], item[2])
257             print((str(i) + '. ' + commodity).ljust(20) + ('单价:' + price + '').ljust(15) + '数量:' + number)
258             total_sum += int(price) * int(number)
259             i += 1
260         print('\n合计:%d 元' % total_sum)
261 
262         while True:
263             if_buy = input('\n是否结算?(Y/N):')
264             if not (if_buy == 'Y' or if_buy == 'y' or if_buy == 'N' or if_buy == 'n'):
265                 print('\n输入有误,请重试...')
266                 continue
267             break
268 
269         while True:
270             # 结算
271             if if_buy == 'Y' or if_buy == 'y':  
272                 count = 0
273                 cancel = False
274 
275                 while count < 3:
276                     user_pay_passwd = getpass.getpass('\n请输入支付密码,或输入 c 放弃支付:')
277                     if user_pay_passwd == 'c':
278                         print('\n取消支付成功...')
279                         cancel = True
280                         break
281                     elif user_pay_passwd != info[3]:
282                         if count != 2:
283                             print('\n密码错误,请重试,您还有 %d 次机会...' % (2-count))
284                         count += 1
285                     else:
286                         break
287 
288                 if cancel:
289                     if_payed = False
290 
291                 elif count == 3:
292                     with open('lock_list.txt', 'w') as lock_file:
293                         lock_file.write(username + '\n')
294                     exit('\n密码错误,账户已被锁定...')
295 
296                 else:
297                     if total_sum <= user_balance:
298                         user_balance -= total_sum
299                         print('\n支付成功!您已成功购买以下商品:\n')
300                         i = 1
301                         for item in shopping_cart:
302                             (commodity, price, number) = (item[0], item[1], item[2])
303                             print((str(i) + '. ' + commodity).ljust(20) +
304                                   ('单价:' + price + '').ljust(15) + '数量:' + number)
305                             lines = open('product_list.txt', 'r').readlines()
306                             for line in lines:  # 修改商品库存
307                                 if commodity in line.split(', '):                    # 定位到商品所在行
308                                     j = lines.index(line)                            # 获取商品所在行的行号索引
309                                     select = line.split(', ')                        # 商品所在行分割为字符串列表
310                                     select[-1] = (str(int(select[-1]) - int(number)) + '\n')    # 修改商品库存
311                                     lines[j] = ', '.join(select)                     # 将修改后的字符串列表组成字符串
312                                     open('product_list.txt', 'w').writelines(lines)  # 把修改写回文件
313                             i += 1
314 
315                         lines = open('user_list.txt', 'r').readlines()
316 
317                         for line in lines:  # 用户余额写入文件
318                             if username in line.split(','):
319                                 j = lines.index(line)
320                                 select = line.split(',')
321                                 select[-2] = str(user_balance)
322                                 lines[j] = ','.join(select)
323                                 open('user_list.txt', 'w').writelines(lines)
324 
325                         exit('\n合计:%d 元, 账户余额:%d 元' % (total_sum, user_balance))
326 
327             # 不结算
328             else:   
329                 print('\n您有一笔未支付订单...')
330                 while True:
331                     choice = input('\n是否进行支付?(Y/N):')
332                     if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'):
333                         print('\n输入有误,请重试...')
334                         continue
335                     break
336                 if choice == 'n' or choice == 'N':
337                     exit('\n订单已取消,感谢光临购物商城,再见...')
338                 else:
339                     if_buy = 'Y'
340                     continue
341 
342             if not if_payed:
343                 print('\n您有一笔未支付订单...')
344                 while True:
345                     choice = input('\n是否进行支付?(Y/N):')
346                     if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'):
347                         print('\n输入有误,请重试...')
348                         continue
349                     break
350 
351                 if choice == 'n' or choice == 'N':
352                     exit('\n订单已取消,感谢光临购物商城,再见...')
353                 else:
354                     if_buy = 'Y'
355                     continue
356 
357 # 商家入口
358 if entrance == '2':     
359 
360     seller_name = ''
361 
362     # 登录接口
363     count = 0
364     while count < 3:
365         seller_name = input('\n用户名:')
366 
367         with open('lock_list.txt', 'r') as lock_file:
368             for lock_line in lock_file:
369                 if seller_name == lock_line.strip('\n'):
370                     exit('\n用户名 %s 已被锁定,请联系管理员...' % seller_name)
371 
372         seller_file = open('seller_list.txt', 'r')
373         login = False
374 
375         for seller_line in seller_file:
376             (seller, passwd) = seller_line.strip('\n').split(',')
377             if seller_name == seller:
378                 n = 0
379                 while n < 3:
380                     password = getpass.getpass('密码:')
381                     # 登录成功,跳出while循环
382                     if password == passwd:
383                         print('\n欢迎 %s 登录商城' % seller_name)
384                         login = True
385                         break   
386                     else:
387                         if n != 2:
388                             print('\n密码错误,请重试,您还有 %d 次机会' % (2-n))
389                     n += 1
390                 # n = 3,锁定用户名
391                 else:           
392                     open('lock_list.txt', 'w').write(seller_name + '\n')
393                     exit('\n错误次数过多,账户已被锁定...')
394                 # 登录成功,跳出for循环
395                 if login:       
396                     break
397 
398         # 用户名不存在
399         else:       
400             if count != 2:
401                 print('\n用户名不存在,请重试,您还有 %d 次机会' % (2-count))
402 
403         # 登录成功,跳出while循环
404         if login:   
405             break
406 
407         count += 1
408 
409     else:
410         exit('\n错误次数过多,程序已退出...')
411 
412 
413     # 商品列表编辑程序
414   
415     L = []
416     # 存放商品列表,初始为空
417     commodity_list = []
418     index = 1
419 
420     print('\n您的货架上有以下商品:\n')
421   
422     print_product_list()
423 
424     while True:
425         choice = input('\n是否编辑您的商品列表?(Y/N):')
426         if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'):
427             print('\n输入有误,请重试...')
428             continue
429         break
430 
431     if choice == 'Y' or choice == 'y':
432         while True:
433             print('\n请选择(输入 q 退出):\n')
434             print('1. 上架新品\n\n2. 下架商品\n\n3. 修改商品信息')
435             choice = input('\n>>>')
436             if not (choice == '1' or choice == '2' or choice == '3' or choice == 'q'):
437                 print('输入有误,请重试...')
438                 continue
439 
440             # 上架新品
441             if choice == '1':
442                 while True:
443                     if_add = False  # 是否添加商品的标志,初始为False
444                     new_commodity = input('\n输入商品名:')
445 
446                     product_file = open('product_list.txt', 'r')
447 
448                     for product_line in product_file:
449                         commodity = product_line.strip('\n').split(', ')[0]     # 获取商品列表中的商品名
450                         if new_commodity == commodity:
451                             print('\n此商品已在货架上...')
452                             continue
453                         else:
454                             while True:
455                                 if_sure = input('\n确定上架新品 %s 吗?(Y/N):' % new_commodity)
456                                 if not (if_sure == 'Y' or if_sure == 'y' or if_sure == 'N' or if_sure == 'n'):
457                                     print('\n输入有误,请重试...')
458                                     continue
459                                 break
460                             # 确定上架新品
461                             if if_sure == 'Y' or if_sure == 'y':
462                                 while True:     # 输入单价
463                                     price = input('\n请输入单价:')
464                                     if not price.isdigit():
465                                         print('\n输入有误,请重试...')
466                                         continue
467                                     break
468                                 while True:     # 输入库存
469                                     stock = input('\n请输入库存:')
470                                     if not stock.isdigit():
471                                         print('\n输入有误,请重试...')
472                                         continue
473                                     break
474                                 new_line = '\n' + new_commodity + ', ' + price + ', ' + stock
475                                 open('product_list.txt', 'a').writelines(new_line)
476                                 print('\n成功上架新品 %s ,单价 %s 元,库存 %s 件' % (new_commodity, price, stock))
477 
478                                 while True:
479                                     option = input('\n是否继续添加?(Y/N):')
480                                     if not (option == 'Y' or option or option == 'N' or option == 'n'):
481                                         print('\n输入有误,请重试...')
482                                         continue
483                                     break
484                                 if option == 'Y' or option == 'y':
485                                     if_add = True
486                                     break   # 跳出for循环
487                                 else:
488                                     break
489                             # 取消上架新品
490                             else:
491                                 if_add = False
492                                 break   # 跳出for循环
493                     product_file.close()
494 
495                     if if_add is True:
496                         continue
497                     else:
498                         break   # 跳出while循环
499 
500             # 下架商品
501             elif choice == '2':
502                 while True:
503                     del_num = input('\n请输入您想下架商品的序号:')
504                     if not (del_num.isdigit() or int(del_num) > 0 and int(del_num) <= len(commodity_list)):
505                         print('\n输入有误,请重试...')
506                         continue
507                     break
508 
509                 del_num = int(del_num)
510                 del_commodity = commodity_list[del_num - 1][0]
511 
512                 with open('product_list.txt', 'r') as old_file:
513                     with open('product_list.txt', 'r+') as new_file:
514 
515                         current_line = 0
516 
517                         # 定位到需要删除的行
518                         while current_line < (del_num - 1):
519                             old_file.readline()
520                             current_line += 1
521 
522                         # 当前光标在被删除行的行首,记录该位置
523                         seek_point = old_file.tell()
524 
525                         # 设置光标位置
526                         new_file.seek(seek_point, 0)
527 
528                         # 读需要删除的行,光标移到下一行行首
529                         old_file.readline()
530                         
531                         # 被删除行的下一行读给 next_line
532                         next_line = old_file.readline()
533 
534                         # 连续覆盖剩余行,后面所有行上移一行
535                         while next_line:
536                             new_file.write(next_line)
537                             next_line = old_file.readline()
538 
539                         # 写完最后一行后截断文件,因为删除操作,文件整体少了一行,原文件最后一行需要去掉
540                         new_file.truncate()
541 
542                 print('\n您已成功下架 %s !' % del_commodity)
543 
544             # 修改商品信息
545             elif choice == '3':
546 
547                 # 修改商品信息
548                 def mod_commodity_info(i, j):
549                     i = int(i)
550                     j = int(j)
551                     with open('product_list.txt', 'r+') as f:
552                         current_line = 0
553                         while current_line < i - 1:
554                             f.readline()
555                             current_line += 1
556                         seek_point = f.tell()
557                         f.seek(seek_point, 0)
558 
559                         # 修改商品名
560                         if j == 1:
561                             update_line = mod_name() + ', ' + commodity_list[i-1][1] + ', ' + commodity_list[i-1][2] + '\n'
562                         # 修改商品价格
563                         elif j == 2:
564                             update_line = commodity_list[i-1][0] + ', ' + mod_price() + ', ' + commodity_list[i-1][2] + '\n'
565                         # 修改商品库存
566                         else:
567                             update_line = commodity_list[i-1][0] + ', ' + commodity_list[i-1][1] + ', ' + mod_stock() + '\n'
568                         
569                         f.write(update_line)
570                     return
571                 
572                 def mod_name():
573                     new_name = input("\n请输入新的商品名:")
574                     return new_name
575 
576                 def mod_price():
577                     new_price = input("\n请输入新的商品单价:")
578                     return new_price
579 
580                 def mod_stock():
581                     new_stock = input("\n请输入新的商品库存:")
582                     return new_stock
583 
584                 # 修改商品单价
585                 def mod_commodity_price(i):
586                     i = int(i)
587                     with open('product_list.txt', 'r+') as f:
588                         current_line = 0
589                         while current_line < i -1:
590                             f.readline()
591                             current_line += 1
592                         seek_point = f.tell()
593                         f.seek(seek_point, 0)
594                         new_price = input()
595 
596 
597                 while True:
598                     i = input("\n请输入需要编辑的商品序号(输入 c 取消):")
599                     if not (i.isdigit or i == 'c' or int(i) > 0 and int(i) <= len(commodity_list)):
600                         print("\n输入有误,请重试...")
601                         continue
602                     elif i == 'c':
603                         break
604                     else:
605                         while True:
606                             j = input("\n请选择需要编辑的选项(输入 c 取消):\n\n1. 商品名\n\n2. 单价\n\n3. 库存\n\n>>>")
607                             if not (j == 'c' or j == '1' or j == '2' or j == '3'):
608                                 print("\n输入有误,请重试...")
609                                 continue
610                             break
611                         if j == 'c':
612                             break
613                         else:
614                             mod_commodity_info(i, j)
615 
616             else:
617                 exit('\n您已退出商城...')
618     else:
619         exit('\n您已退出商城...')

 

转载于:https://www.cnblogs.com/Dreamer-qiao/p/7522952.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以使用 GUI 库如 tkinter, PyQt, wxPython 等来创建图形用户界面。具体的实现方式可以根据您的需求和熟悉的库选择。 以 tkinter 为例,您可以这样写一个图形用户界面的入口: ``` import tkinter as tk root = tk.Tk() root.title("My GUI") root.mainloop() ``` 这段代码会创建一个简单的图形界面,窗口标题为 "My GUI"。您可以通过添加控件(如按钮、文本框等)来扩展这个界面的功能。 ### 回答2: 要用Python编写一个图形用户界面(GUI)入口,可以使用Python的内置库Tkinter(也称为Tk)。 首先,你需要先导入Tkinter模块: ```python import tkinter as tk ``` 然后,你可以创建一个窗口的实例: ```python window = tk.Tk() ``` 接下来,你可以设置窗口的标题和大小: ```python window.title("GUI 入口") window.geometry("300x200") ``` 要向窗口添加其他组件,例如按钮、文本框等,可以使用Tkinter提供的不同组件类,例如Button和Entry。以下是一个示例添加按钮的代码: ```python button = tk.Button(window, text="点击我") button.pack() ``` 最后,你需要运行主循环以显示窗口和交互。通过调用以下代码来实现: ```python window.mainloop() ``` 完整的示例代码如下所示: ```python import tkinter as tk window = tk.Tk() window.title("GUI 入口") window.geometry("300x200") button = tk.Button(window, text="点击我") button.pack() window.mainloop() ``` 运行这段代码将会显示一个带有一个按钮的窗口。当你点击按钮时,可以通过相应的代码进行相应的操作。 希望这个回答对你有所帮助! ### 回答3: 要用Python编写一个图形用户界面(GUI)入口,你可以使用Tkinter模块,它是Python的标准GUI库。 首先,你需要导入Tkinter模块并创建一个主窗口。然后,你可以通过添加其他GUI组件(如按钮、标签、文本框等)来构建用户界面。最后,你需要使用主循环来显示用户界面并处理事件。 下面是一个简单的示例代码,演示如何使用Tkinter创建一个GUI入口: ```python import tkinter as tk def on_button_click(): print("按钮被点击了!") # 创建主窗口 window = tk.Tk() # 添加按钮 button = tk.Button(window, text="点击我", command=on_button_click) button.pack() # 显示主窗口 window.mainloop() ``` 在上面的示例中,我们首先导入了Tkinter模块并创建了一个主窗口。然后,我们定义了一个回调函数`on_button_click()`,该函数在按钮被点击时会被调用,并打印一条消息。接下来,在主窗口中添加了一个按钮,该按钮的文本是“点击我”,并指定了点击按钮时要调用的函数为`on_button_click`函数。最后,我们通过调用`window.mainloop()`来显示主窗口并启动事件处理循环。 使用这个简单的示例,你可以开始构建自己的图形用户界面入口。你可以根据需要添加其他的GUI组件、布局管理和功能。希望这个示例对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值