macos下使用 tkinter 提示异常 your Python may not be configured for Tk, No module named ‘msilib‘ 问题解决方法

原因是对应版本的python-tk没有安装, 安装对应的 python-tk 即可成功导入和使用.  如你安装的python版本为3.11, 就需要执行 brew install python-tk@3.11 安装3.11版本的tk

macos下的安装方法为

brew install python-tk

 注意需要和你安装的python版本对应, 可以先使用 brew search python-tk 来搜索可用的python-tk包的版本.

brew search python-tk
==> Formulae
python-tk@3.10     python-tk@3.9       python@3.7          python@3.9 
python-tk@3.11      python-yq           python@3.8

如果你安装的python版本为3.11 就需要安装 python-tk@3.11 , 如果版本不对应你在使用venv环境时是找不到 tk包的

brew install python-tk@3.11

没有安装python-tk 还会导致 msilib模块未找到无法使用 Listbox 的错误, 这个模块默认只存在于win32系统中可用

ModuleNotFoundError: No module named 'msilib'

完整示例代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tkinter import *

root = Tk()

# height 默认显示10条数据 listBox = Listbox(root, height=11)

# 为了在某个组件上安装垂直滚动条,需要做两件事:
# 1.设置该组件的yscrollcommand选项为Scrollbar组件的set()方法
# 2.设置Scrollbar组件的command选项为该组件的yview()方法

sb = Scrollbar(root)
sb.pack(side=RIGHT, fill=Y)

listBox = Listbox(root, yscrollcommand=sb.set)

listBox.insert(0, TkVersion)
for i in range(100):
    listBox.insert(END, i)  # ListBox添加数据

listBox.pack()
listBox.see(20)  # 调整列表框的位置,使得 index 参数指定的选项是可见的

sb.config(command=listBox.yview)


def delete():
    listBox.delete(ACTIVE)  # 删除选中的

delButton = Button(root, text="删除", command=delete)
delButton.pack(padx=10, pady=10)

root.mainloop()

成功解决tkinter 

以下是 Python 3.9 版本的标准库列表: - abc - aifc - argparse - array - ast - asyncio - asyncore - atexit - audioop - base64 - bdb - binascii - binhex - bisect - builtins - bz2 - cProfile - calendar - cgi - cgitb - chunk - cmath - cmd - code - codecs - codeop - collections - collections.abc - colorsys - compileall - concurrent - configparser - contextlib - contextvars - copy - copyreg - crypt - csv - ctypes - curses - dataclasses - datetime - dbm - decimal - difflib - dis - distutils - doctest - email - encodings - ensurepip - enum - errno - faulthandler - fcntl - filecmp - fileinput - fnmatch - formatter - fractions - ftplib - functools - gc - getopt - getpass - glob - grp - gzip - hashlib - heapq - hmac - html - http - hyperlinks - imaplib - imghdr - importlib - inspect - io - ipaddress - itertools - json - keyword - lib2to3 - linecache - locale - logging - lzma - mailbox - mailcap - marshal - math - mmap - modulefinder - msilib - multiprocessing - netrc - nis - nntplib - numbers - opcode - operator - optparse - os - os.path - parser - pathlib - pdb - pickle - pickletools - pipes - pkgutil - platform - plistlib - poplib - pprint - profile - pstats - pty - py_compile - queue - quopri - random - re - readline - reprlib - resource - rlcompleter - runpy - sched - secrets - select - selectors - shelve - shlex - shutil - signal - site - smtpd - smtplib - sndhdr - socket - socketserver - sqlite3 - ssl - stat - statistics - string - stringprep - struct - subprocess - sunau - symbol - symtable - sys - sysconfig - syslog - tabnanny - tarfile - telnetlib - tempfile - termios - test - textwrap - threading - time - timeit - tkinter - token - tokenize - traceback - tracemalloc - tty - turtle - types - typing - unicodedata - unittest - urllib - uu - uuid - venv - warnings - wave - weakref - webbrowser - winreg - winsound - wsgiref - xdrlib - xml - xmlrpc - zipapp - zipfile - zipimport - zlib
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值